CKTIS / JavaScript
Object property shorthand, concise methods, and computed access
Distinguish shorthand properties and concise methods in object literals from reading, writing, and calling properties with keys determined at runtime.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
const obj = {
x,
[key]: value,
method(arg) { /* body */ },
[methodKey](arg) { /* body */ },
};
obj.x;
obj[key];
obj[key] = value;
obj.method(arg);
obj[methodKey](arg);Object shorthand/method and computed access syntax covers both defining properties and accessing them. In an object literal, { x } is shorthand for { x: x }: it creates an own data property named "x" with the current value of the variable x. Reassigning the variable later does not update the property. If the variable does not exist, evaluating the literal throws ReferenceError.
method(arg) { … } defines a method without function. Unlike method: function (arg) { … } with an ordinary function, the concise method is not a constructor (new obj.method() throws TypeError) and can use super to look up properties on the prototype of the object where it was defined. In obj.method(arg) and obj[methodKey](arg), obj is the this receiver. Storing the method in a variable does not preserve that receiver.
{ [key]: value } and { [methodKey](arg) { … } } compute a key when the object is created. obj[key] computes a key each time the access runs, whereas obj.key always names the property "key". Keys are strings or Symbol values; for example, the number 1 becomes "1". Brackets also work for names that dot notation cannot express, such as obj["bonus-points"].
obj[key] reads a property, and obj[key] = value assigns to it. With ordinary property lookup, a read returns undefined if the key is absent from both the object and its prototype chain. If the object itself or an object in its prototype chain is a Proxy, its get trap can return a different value even when no property with that key exists. Accessing null or undefined throws TypeError. Check untrusted keys against an allowlist before dynamic access, especially before writes.
In a literal, { ["__proto__"]: value } creates an ordinary own data property, while { __proto__: value } is special syntax that sets the prototype when value is an object or null. These forms are part of ECMAScript 2024; availability depends on the runtime.
Examples
{
const x = 2;
const key = "bonus-points";
const methodKey = Symbol("addBonus");
const obj = {
x,
[key]: 3,
total() {
return this.x + this[key];
},
[methodKey](amount) {
this[key] += amount;
return this.total();
},
};
console.log(obj.total());
console.log(obj[methodKey](4));
console.log(obj[key]);
console.log(obj.key);
}Result
5 9 7 undefined
`x` is `2` and `"bonus-points"` starts at `3`. Calling the method through its `Symbol` key raises the bonus to `7` and returns a total of `9`. `obj.key` looks up the literal name `"key"`, so it returns `undefined`.
Related lessons
- JavaScript · Lesson 30 · Advanced · Version: 4