Fandom

JavaScript Wiki

Object

87pages on
this wiki
Add New Page
Talk17 Share

A JavaScript object is a container of key-value pairs, known as 'maps', 'dictionaries', or 'associative arrays' in other languages. The keys are always strings, and lookups can be performed with the identifier lookup or string lookup operators. The Object is fundamental to Javascript, and is the basis of all other non-primitive types, as well as being vital to JavaScript's object-orientation model. Excluding object representations of primitives, all objects are represented as 'object' by typeof.

References ECMA Mozilla Microsoft
Literals [1] [2] [3]
Objects [4] [5][6] [7]


Literal syntaxEdit

DefinitionEdit

An object is created by surrounding a list of comma-separated key : value pairs with braces. The key must be a quoted string or an identifier (which will be converted to a string); the value can be any expression. Caveat: In positions where a block is allowed, objects must be surrounded with parentheses to avoid ambiguity, or errors will result.

  1. ({ foo : 5 + 3 })    // Equivalent to #2
  2. ({ "foo" : 5 + 3 })  // Equivalent to #1
  3. // ({ foo&bar : 5 }) // Illegal
  4. ({ "foo&bar" : 5 })  // Legal

OperatorsEdit

Lookup operatorsEdit

Since objects are defined as a collection of key/value pairs, there must be operators to get and set those pairs. These are the lookup operators, and they are specific to objects (although primitive values are usually converted to an appropriate object instance when a lookup operator is applied).

Lookup expressions form a sort of variable; it's possible to assign to them, setting the property. New properties can be created simply by assigning to a non-existent property. The delete operator must be used to completely remove a key from an object.

Attempting to look up a non-existent property will yield undefined.

. (identifier lookup)Edit

If the name of a property is a valid identifier, it can be looked up by providing the object and the identifier directly (unquoted) to the . operator.

({ foo: 5 }).foo // 5
({}).foo         // undefined

[] (string lookup)Edit

It is also possible to supply a string value using the string lookup operator. This allows access of properties via dynamically-generated names, such as iterations of a for..in loop. Unlike the identifier lookup, the name need not be a valid identifier; this is most often used in arrays, where the property names are numerical.

({ foo: 5 })['foo']                                             // 5
({ "1 invalid identifier!" : "fish" })['1 invalid identifier!'] // "fish"

Comparison operatorsEdit

These operators take two objects and return a boolean value.

=== (equals)Edit

Returns true if both operands are identical (references to the same object); otherwise, returns false. It does not compare structure: two different objects that are structurally identical will not be considered equal.

({ foo: 5 }) === ({ foo: 5 })        // false: these two are not the same object!
 
var a = { foo: 5 }, b = a; a === b   // true: a and b are references to the same object.

!== (not equals)Edit

Checks whether objects are non-identical. a !== b is equivalent to !(a === b).

({ foo: 5 }) !== ({ foo: 5 })        // true
 
var a = { foo: 5 }, b = a; a !== b   // false

MembersEdit

These properties and methods are available on the global Object constructor.

prototypeEdit

Data type object
Standard ECMA-262 §15.2.3.1
Documentation Mozilla, Microsoft

These members are available on each instance of Object. (This includes object literals.)

constructorEdit

Data type function
Standard ECMA-262 §15.2.4.1
Documentation Mozilla, Microsoft

Points to the function used to construct the object. For direct Object instances, this will be Object.

Caveat: Not available in WebKit/KHTML.

toStringEdit

Data type function
Return type string
Parameter list none
Standard ECMA-262 §15.2.4.2
Documentation Mozilla Microsoft

Returns a string representation of the object. Default is "[object ConstructorName]", where ConstructorName is the name of the constructor that created this object.

hasOwnPropertyEdit

Data type function
Return type boolean
Parameter list key (string)
Standard ECMA-262 §15.2.4.5
Documentation Mozilla, Microsoft

Checks whether the object itself (as opposed to a prototype) has a property associated with the given key.

isPrototypeOfEdit

Data type function
Return type boolean
Parameter list none
Standard ECMA-262 §15.2.4.6
Documentation Mozilla Microsoft

Returns true if this object is the prototype of the provided obj; otherwise, returns false.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.