Thoughts on Object.creat() triggered by a written test question

I took part in Kujiale's written test this evening. There is a multiple-choice question that made me hesitate a bit. Now I put it out for everyone to think about first.

(The code I typed out from memory, there is a mistake in Yazheng)

Based on the above code, we may first wonder whether a and b refer to the same object. From the method of Object.create( ), it should create a new object, so it should not refer to the same object. , then excludes the possibility of referencing the same object. Is it possible that it is the copying of the object? If we regard this process as the copying , then the output of the console should be: 1,3

 

We now publish the console output:

Is the result a little different from what we expected? Let's go to MDN to see how it explains Object.create().

 

The following is the official explanation from MDN:

Object.create()method creates a new object, using the existing object to provide the __proto__ of the newly created object. 

 

Therefore, according to the explanation of the official document, we can understand that the prototype of the created new object b points to a. The b.foo printed at the beginning is not b's own foo attribute, but a's foo attribute. To prove this It's very easy, we can use the hasOwnProperty method to check if foo is a property of b itself.

Below is the example code

Console output:

The result is very obvious, and it also proves that it is not the foo attribute of b itself that is printed at the beginning, but the foo attribute of a, and then b.foo = 3; only b has its own foo attribute, so it is printed as 3.

 

  Regarding the Object.create( ) method, its usage is more than that. It can be used to implement the idea of ​​inheritance in Java (through the prototype chain), and the same happens when we create an object through new Phenomenon, that is, the prototype of the new new object will point to the prototype property of the function that constructs the object, and the prototype property itself is an object, and the middle point actually constitutes a prototype chain, we are calling an object's property or When a method is used, it will first start looking for its own properties, and if it cannot be found, it will look up along the prototype chain. So when we create an "empty" object with the literal var a = { };, we can also make it equivalent to var a = Object.create(Object.prototype );

 

Note: The "empty" here is double-quoted to indicate that a is not actually an empty object, it just doesn't have its own properties, but it can call the methods defined by Object.prototype, and Object.create( null ) is a For a completely empty object, it cannot call the method defined by Object.prototype, because the object prototype it creates points to null, so if you want to create an "empty" object, you must remember not to use Object.create(null).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325096705&siteId=291194637