JavaScript Objects

From Code Self Study Wiki
Jump to: navigation, search

This page contains notes about JavaScript objects.

Reading[edit]

Basics[edit]

Object values are by reference. If you copy an object into a new variable and change the new variable name, the old name will change too.

Attributes[edit]

  • Configurable Attribute
  • Enumerable
  • Writable

Every object also has:

  • constructor
  • hasOwnProperty(propertyName)
  • isPrototypeOf(object)
  • propertyIsEnumerable(propertyName)
  • toLocaleString()
  • toString()
  • valueOf()

Creating Objects[edit]

Ways to create JavaScript objects:

Object Literals[edit]

var box = {
  color: "Green",
  width: 50,
  height: 50
}

Object Constructors[edit]

Use the new keyword.

var person = new Object();
person.name = "John";
person.sayHello = function() {
  console.log("Hello");
}