Method definitions
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.
Method definition is a shorter syntax for defining a function property in an object initializer. It can also be used inclasses.
In this article
Try it
const obj = { foo() { return "bar"; },};console.log(obj.foo());// Expected output: "bar"Syntax
({ 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:
const obj = { foo: function () { // … }, bar: function () { // … },};You are now able to shorten this to:
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.
const obj = { method() {},};new obj.method(); // TypeError: obj.method is not a constructorUsing 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.
const obj = { __proto__: { prop: "foo", }, notAMethod: function () { console.log(super.prop); // SyntaxError: 'super' keyword unexpected here },};Examples
>Using method definitions
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.
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.
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.
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()); // 2Generator methods
Note that the asterisk (*) in the generator method syntax must bebefore the generator property name. (That is,* g(){} will work, butg *(){} will not.)
// 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); // 1Async methods
// Using a named propertyconst obj = { f: async function () { await somePromise; },};// The same object using shorthand syntaxconst obj2 = { async f() { await somePromise; },};Async generator methods
// 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> |