CKTIS / JavaScript
Object method definition syntax
Use `method(param) { ... }` to define an object-literal method without `:` or `function`. The call determines `this`, but the method’s definition determines where `super` looks.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
const obj = {
method(param) {},
[keyExpression](param) {},
*generator() {},
async fetchValue() {},
async *streamValues() {},
get value() { return this._value; },
set value(next) { this._value = next; },
};Object method definition syntax lets you write method(param) { ... } inside an object literal. It creates an own data property whose value is a function; the property is writable, enumerable, and configurable. Use [keyExpression] in place of the name to compute the key when the object is created. The resulting key is a string or a symbol.
Calling a generator method (*method() { ... }) returns a generator iterator. Calling an async method (async method() { ... }) returns a Promise, and calling an async generator method (async *method() { ... }) returns an async iterator. All of these forms accept computed keys. By contrast, get value() { ... } and set value(next) { ... } define accessors that run on property reads and assignments, respectively, without a value() call.
In obj.method(), this is obj. Reading the function does not bind it to the object: if you extract the method and call it as a plain function, the original receiver is not passed. Use fn.call(obj) to supply it explicitly. An arrow function stored in a property instead takes this from its surrounding scope.
super.prop and super[key] start lookup at the current prototype of the object where the method was defined ([[HomeObject]]). Copying the function to another object does not change that starting point, even though it can change this. A call such as super.method() passes the current this to the function it finds. The constructor call super() is not allowed in an object-literal method.
method() { ... } is not fully equivalent to method: function () { ... }. Using new obj.method() on a concise method throws TypeError, whereas an ordinary synchronous, non-generator function expression can be used as a constructor. Writing super.prop directly in the body of such a function expression is a SyntaxError.
Examples
{
const key = 'describe';
const base = {
label() { return `base:${this.name}`; },
};
const original = {
__proto__: base,
name: 'A',
[key]() { return super.label(); },
regular: function () { this.value = 'ordinary'; },
};
const other = { name: 'B', describe: original.describe };
console.log(original.describe());
console.log(other.describe());
try {
new original.describe();
} catch (error) {
console.log(error.name);
}
console.log(new original.regular().value);
}Result
base:A base:B TypeError ordinary
The literal’s `__proto__: base` sets the prototype of `original`. Both calls find `base.label` through `super`, but `this.name` is `A` on the first call and `B` on the second. `new` throws `TypeError` for `describe`; the ordinary function `regular` constructs an object whose `value` is `ordinary`.
Related lessons
- JavaScript · Lesson 20 · Advanced · Version: 4