Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Method definitions

BaselineWidely available

Method definition is a shorter syntax for defining a function property in an object initializer. It can also be used inclasses.

Try it

const obj = {  foo() {    return "bar";  },};console.log(obj.foo());// Expected output: "bar"

Syntax

js
({  property(parameters) {},  *generator(parameters) {},  async property(parameters) {},  async *generator(parameters) {},  // with computed keys  [expression](parameters) {},  *[expression](parameters) {},  async [expression](parameters) {},  async *[expression](parameters) {},})

Description

The shorthand syntax is similar to thegetter andsetter syntax.

Given the following code:

js
const obj = {  foo: function () {    // …  },  bar: function () {    // …  },};

You are now able to shorten this to:

js
const obj = {  foo() {    // …  },  bar() {    // …  },};

Properties defined using this syntax are own properties of the created object, and they are configurable, enumerable, and writable, just like normal properties.

function*,async function, andasync function* properties all have their respective method syntaxes; see examples below.

However, note that the method syntax is not equivalent to a normal property with a function as its value — there are semantic differences. This makes methods defined in object literals more consistent with methods inclasses.

Method definitions are not constructable

Methods cannot be constructors! They will throw aTypeError if you try to instantiate them. On the other hand, a property created as a function can be used as a constructor.

js
const obj = {  method() {},};new obj.method(); // TypeError: obj.method is not a constructor

Using super in method definitions

Only functions defined as methods have access to thesuper keyword.super.prop looks up the property on the prototype of the object that the method was initialized on.

js
const obj = {  __proto__: {    prop: "foo",  },  notAMethod: function () {    console.log(super.prop); // SyntaxError: 'super' keyword unexpected here  },};

Examples

Using method definitions

js
const obj = {  a: "foo",  b() {    return this.a;  },};console.log(obj.b()); // "foo"

Method definitions in classes

You can use the exact same syntax to define public instance methods that are available on class instances. In classes, you don't need the comma separator between methods.

js
class ClassWithPublicInstanceMethod {  publicMethod() {    return "hello world";  }  secondPublicMethod() {    return "goodbye world";  }}const instance = new ClassWithPublicInstanceMethod();console.log(instance.publicMethod()); // "hello world"

Public instance methods are defined on theprototype property of the class and are thus shared by all instances of the class. They are writable, non-enumerable, and configurable.

Inside instance methods,this andsuper work like in normal methods. Usually,this refers to the instance itself. In subclasses,super lets you access the prototype of the object that the method is attached to, allowing you to call methods from the superclass.

js
class BaseClass {  msg = "hello world";  basePublicMethod() {    return this.msg;  }}class SubClass extends BaseClass {  subPublicMethod() {    return super.basePublicMethod();  }}const instance = new SubClass();console.log(instance.subPublicMethod()); // "hello world"

Static methods and private methods use similar syntaxes, which are described in thestatic andprivate elements pages.

Computed property names

The method syntax also supportscomputed property names.

js
const bar = {  foo0: function () {    return 0;  },  foo1() {    return 1;  },  ["foo".toUpperCase()]() {    return 2;  },};console.log(bar.foo0()); // 0console.log(bar.foo1()); // 1console.log(bar.FOO()); // 2

Generator methods

Note that the asterisk (*) in the generator method syntax must bebefore the generator property name. (That is,* g(){} will work, butg *(){} will not.)

js
// Using a named propertyconst obj = {  g: function* () {    let index = 0;    while (true) {      yield index++;    }  },};// The same object using shorthand syntaxconst obj2 = {  *g() {    let index = 0;    while (true) {      yield index++;    }  },};const it = obj2.g();console.log(it.next().value); // 0console.log(it.next().value); // 1

Async methods

js
// Using a named propertyconst obj = {  f: async function () {    await somePromise;  },};// The same object using shorthand syntaxconst obj2 = {  async f() {    await somePromise;  },};

Async generator methods

js
// Using a named propertyconst obj = {  f: async function* () {    yield 1;    yield 2;    yield 3;  },};// The same object using shorthand syntaxconst obj2 = {  async *f() {    yield 1;    yield 2;    yield 3;  },};

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-method-definitions

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp