JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

9.5.3 The Constructor Name

Also, JavaScript does not require that every object have a constructor property: this is a convention based on the default prototype object created for each function, but it is easy to accidentally or intentionally omit the constructor property on the prototype. The first two classes in this chapter, for example, were defined in such a way (in Examples 9-1 and 9-2 ) that their instances did not have constructor properties.

9.5.3 The Constructor Name

The main problem with using the instanceof operator or the constructor property for determining the class of an object occurs when there are multiple execution contexts and thus multiple copies of the constructor functions. These functions may well be identical, but they are distinct objects and are therefore not equal to each other.

One possible workaround is to use the name of the constructor function as the class identifier rather than the function itself. The Array constructor in one window is not equal to the Array constructor in another window, but their names are equal. Some JavaScript implementations make the name of a function available through a nonstandard nameproperty of the function object. For implementations without a nameproperty, we can convert the function to a string and extract the name from that. (We did this in §9.4 when we showed how to add a getName() method to the Function class.)

Example 9-4 defines a type() function that returns the type of an object as a string. It handles primitive values and functions with the typeof operator. For objects, it returns

9.5 Classes and Types | 211

either the value of the class attribute or the name of the constructor. The type()function uses the classof() function from Example 6-4 and the Function.getName() method from §9.4 . The code for that function and method are included here for simplicity.

Example 9-4. A type() function to determine the type of a value


*Return the type of o as a string: * -If o is null, return "null", if o is NaN, return "nan". * -If typeof returns a value other than "object" return that value.*(Note that some implementations identify regexps as functions.) * -If the class of o is anything other than "Object", return that. * -If o has a constructor and that constructor has a name, return it. * -Otherwise, just return "Object". **/

function type(o) { var t, c, n; // type, class, name

// Special case for the null value: if (o === null) return "null";

// Another special case: NaN is the only value not equal to itself: if (o !== o) return "nan";

// Use typeof for any value other than "object". // This identifies any primitive value and also functions. if ((t = typeof o) !== "object") return t;

// Return the class of the object unless it is "Object". // This will identify most native objects. if ((c = classof(o)) !== "Object") return c;

// Return the object's constructor name, if it has one if (o.constructor && typeof o.constructor === "function" && (n = o.constructor.getName())) return n;

// We can't determine a more specific type, so return "Object" return "Object"; }

// Return the class of an object. function classof(o) { return,-1); };

// Return the name of a function (may be "") or null for nonfunctions

Function.prototype.getName = function() { if ("name" in this) return; return = this.toString().match(/function\s*([^(]*)\(/)[1];


This technique of using the constructor name to identify the class of an object has one of the same problems as using the constructor property itself: not all objects have a constructor property. Furthermore, not all functions have a name. If we define a constructor using an unnamed function definition expression, the getName() method will return an empty string:

友情链接It题库(| 版权归yishouce.com所有| 友链等可联系|粤ICP备16001685号-1