Object come in two forms
- the declarative form (litral)
- Constrcted form
Declarative/litral Object
var myObj = {
key:value
// ...
}
Constrcted form
var myObj = new Object();
myObj.key = value;
Example
var strPrimitive = "I am a string";
console.log( strPrimitive.length ); // 13
console.log( strPrimitive.charAt( 3 ) ); // "m"
In both cases, we call a property or method on a string primitive, and the engine automatically coerces it to a String object, so that the property/method access works.
Build-in Objects There are several other object subtypes, usually referred to as built-in objects.
- String
- Number
- Boolean
- Object
- Function
- Array
- Date
- RegExp
- Error
To access the value at the location a in myObject, we need to use either
the .
operator or the [ ]
operator. The .a syntax is usually referred to
as “property access,” whereas the ["a"]
syntax is usually referred to as
“key access.” In reality, they both access the same location and will pull
out the same value, 2, so the terms can be used interchangeably. We
will use the most common term, “property access,” from here on.
Special Values
Null is an empty value undegined is a missing value