Stats

1405 Contributors: 54 Wednesday, July 26, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

Objects

Syntax

  • object = { }
  • object = new Object()
  • object = Object.create(prototype[, propertiesObject])
  • object.key = value
  • object["key"] = value
  • object[Symbol()] = value
  • object = { key1: value1, "key2": value2, 'key3': value3 }
  • object = { conciseMethod() { … } }
  • object = { [computed() + "key"]: value }
  • Object.defineProperty(obj, propertyName, propertyDescriptor)
  • property_desc = Object.getOwnPropertyDescriptor(obj, propertyName)
  • Object.freeze(obj)
  • Object.seal(obj)

Parameters

PropertyDescription
valueThe value to assign to the property.
writableWhether the value of the property can be changed or not.
enumerableWhether the property will be enumerated in for in loops or not.
configurableWhether it will be possible to redefine the property descriptor or not.
getA function to be called that will return the value of the property.
setA function to be called when the property is assigned a value.

Remarks

Objects are collections of key-value pairs, or properties. The keys can be Strings or Symbols, and values are either primitives (numbers, strings, symbols) or references to other objects.

In JavaScript, a significant amount of values are objects (e.g. functions, arrays) or primitives that behave as immutable objects (numbers, strings, booleans). Their properties or their prototype's properties can be accessed using dot (obj.prop) or bracket (obj['prop']) notation. Notable exceptions are the special values undefined and null.

Objects are held by reference in JavaScript, not by value. This means that when copied or passed as arguments to functions, the "copy" and the original are references to the same object, and a change to one's properties will change the same property of the other. This does not apply to primitives, which are immutable and passed by value.

Related Examples