HTML and CSS Reference
In-Depth Information
Listing 7.2 A property name with spaces
var testMethods = {
"test dots and brackets should behave identically":
function () {
var value = "value";
var obj = { prop: value };
assertEquals(obj.prop, obj["prop"]);
}
};
// Grab the test
var name = "test dots and brackets should behave identically";
var testMethod = testMethods[name];
// Mix dot and bracket notation to get number of expected
// arguments for the test method
var argc = testMethods[name].length;
Here we get a test method (i.e., a property) from our object using the square
bracket notation, because the name of the property we are interested in contains
characters that are illegal in identifiers.
It is possible to get and set properties on an object using other values than
string literals, number literals, or identifiers. When you do so, the object will be
converted to a string by its toString method if it exists (and returns a string), or
its valueOf method. Beware that these methods may be implementation-specific
(e.g., for host objects 1 ), and for generic objects the toString method will return
"[object Object]" . I recommend you stick to identifiers, string literals, and
number literals for property names.
7.1.2 The Prototype Chain
In JavaScript every object has a prototype. The property is internal and is referred
to as [[Prototype]] in the ECMA-262 specification. It is an implicit reference to the
prototype property of the constructor that created the object. For generic objects
this corresponds to Object.prototype . The prototype may have a prototype of
its own and so on, forming a prototype chain . The prototype chain is used to share
properties across objects in JavaScript, and forms the basis for JavaScript's inheri-
tance model. This concept is fundamentally different from classical inheritance, in
1. Host objects will be discussed in Chapter 10, Feature Detection.
 
Search WWH ::




Custom Search