Skip to content

Object literals — shorthand, computed keys and methods

Level: 101 · for anyone who has written { key: value }

Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.

One line: An object literal is more than pairs: { name } copies a variable in, [expr]: v computes a key at run time, and __proto__: p sets the prototype instead of making a key — unless written ["__proto__"]: p, which makes an ordinary property.

Keywords: object literal, shorthand property, computed property name, __proto__

What the finished page will answer

  • What does { name, [field + "Max"]: 10, greet() {} } produce, and when is the computed key evaluated?
  • What is the difference between __proto__: p, "__proto__": p and ["__proto__"]: p in a literal?
  • What happens when a literal repeats a key, and why is repeating __proto__: a SyntaxError before the program runs?
  • How does a method written greet() {} differ from greet: function () {}: can you call new on it, and can it use super?
  • Can a value in a literal refer to the object being built, as in const o = { a: 1, b: o.a }?

Examples it will need

  • object_literals_shorthand_js.js — a literal using a shorthand property, a computed key and a method, then Object.keys and the value of each part
  • object_literals_proto_key_js.js — the three spellings of proto in a literal, with the prototype and the own keys each one produces
  • object_literals_duplicate_proto_sh.sh — a literal that repeats proto, rejected with a SyntaxError before its first line runs, and node's exit status

See also

Sources to start from