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

6.1.4 Object.create()

6.1.4 Object.create()

ECMAScript 5 defines a method, Object.create(), that creates a new object, using its first argument as the prototype of that object. Object.create() also takes an optional second argument that describes the properties of the new object. This second argument is covered in §6.7 .

Object.create() is a static function, not a method invoked on individual objects. To use it, simply pass the desired prototype object:

var o1 = Object.create({x:1, y:2}); // o1 inherits properties x and y.

You can pass null to create a new object that does not have a prototype, but if you do this, the newly created object will not inherit anything, not even basic methods like toString() (which means it won’t work with the + operator either):

var o2 = Object.create(null); // o2 inherits no props or methods.

If you want to create an ordinary empty object (like the object returned by {} or new Object()), pass Object.prototype:

var o3 = Object.create(Object.prototype); // o3 is like {} or new Object().

The ability to create a new object with an arbitrary prototype (put another way: the ability to create an “heir” for any object) is a powerful one, and we can simulate it in ECMAScript 3 with a function like the one in Example 6-1 .1

Example 6-1. Creating a new object that inherits from a prototype

// inherit() returns a newly created object that inherits properties from the // prototype object p. It uses the ECMAScript 5 function Object.create() if // it is defined, and otherwise falls back to an older technique. function inherit(p) {

if (p == null) throw TypeError(); // p must be a non-null object

if (Object.create) // If Object.create() is defined...

return Object.create(p); // then just use it.

var t = typeof p; // Otherwise do some more type checking

if (t !== "object" && t !== "function") throw TypeError();

function f() {}; // Define a dummy constructor function.

f.prototype = p; // Set its prototype property to p.

return new f(); // Use f() to create an "heir" of p. }

The code in the inherit() function will make more sense after we’ve covered constructors in Chapter 9 . For now, please just accept that it returns a new object that inherits the properties of the argument object. Note that inherit() is not a full replacement for Object.create(): it does not allow the creation of objects with null prototypes, and it does not accept the optional second argument that Object.create() does. Nevertheless, we’ll use inherit() in a number of examples in this chapter and again in Chapter 9 .

One use for our inherit()function is when you want to guard against unintended (but nonmalicious) modification of an object by a library function that you don’t have control over. Instead of passing the object directly to the function, you can pass an heir. If the function reads properties of the heir, it will see the inherited values. If it sets properties, however, those properties will only affect the heir, not your original object:

var o = { x: "don't change this value" }; library_function(inherit(o)); // Guard against accidental modifications of o

To understand why this works, you need to know how properties are queried and set in JavaScript. These are the topics of the next section.

1. Douglas Crockford is generally credited as the first to propose a function that creates objects in this way. See .

6.1 Creating Objects | 119

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