3. Create Object Object.create
@ Wu Gong Adam
Look at these two days the students ' feedback still feel more difficult to understand, therefore, decided to give an example to help you understand it, the following about scope and closure of the wonderful content of closure had to write later.
In fact, this series is not for beginners, but for those intermediate JS programmers, have a certain understanding of JS, I hope this series can help them through the phenomenon to see the essence. At the same time, this content is highly condensed, to 1, 2 basically corresponds to the content of Javascript:the good parts two chapters, therefore, the proposal interested students, preferably according to the above example procedures, with debugger, careful experience may be able to understand, It is not realistic to expect to see this blog series fully understand the JS object system.
In addition, if you simply write a page, you may not need to make JS understand so clearly, but if you really value JS, intend to apply such as node.js,backbone.js such JS framework, then, I think it is necessary to really get JS to understand.
Also, before you read the following, read 1, 2:
1. Object http://blog.csdn.net/adwu73/article/details/7219044
2. Function Object http://blog.csdn.net/adwu73/article/details/7219887
Well, cut to the chase, in the javascript:the good parts, gives a common way to create objects:
Object.create = function (o) {
var F = function () {};
F.prototype = O;
return new F ();
};
var b=object.create (a);
With the 1,2 section, let's understand this code:
First, before the execution of Var b=object.create (a), there is already a object, the following figure:
After the var f = function () {} is executed, the F function object is created and his property attribute is assigned to a newly created object, as detailed rules are described in section two:
When a function is defined with the var ff = function () {}, JS generates a function object F, the __proto__ pointer to the function object F is pointed to Function.prototype (omitted in the following illustration), and F also has a property attribute. The value of the property attribute points to a newly created object P, the object P has only one property is the constructor constructor (), the value of the constructor constructor () is the function object F, and the __proto__ of the object P The pointer points to Object.prototype,
F.prototype was executed = O; After that, the prototype property of F points to object A, as shown in the following illustration:
After executing new F (), following the rules described in section 2nd, the object relationships after execution are as follows:
When you activate a function with new, the object is created to perform the return result of the function, and the object's __proto__ link points to the object referenced by the function's prototype property.
After the function object.create returns, neither F nor p exists, so the final object relationship is as follows:
To sum up, we can see that the object's __proto__ link cannot be modified directly, and the prototype property of the function object can be modified, so Object.create actually took advantage of this feature, combining new to complete a copy creation process, I hope this example can help you better understand.
You can continue reading 4. Scope and Object http://blog.csdn.net/adwu73/article/details/7229564
Remember: recommend you to look at this blog, explain the will be more detailed, there are more examples: http://www.cnblogs.com/TomXu/archive/2011/12/15/2288411.html