[Remedy] JS Soul Question No. 18 Cultivation of Internal Strength Object.creat() Foundation

introduction

I haven't updated the column series of JS Soul Questions for a few days. In the autumn recruitment season, I am also busy preparing for the written interview. Today I have time to write an article again. The content of this article is about Object.creat()基础, so if I can ask you questions today, let’s explore it together.

Those who look up at the stars should not be laughed at

Object.creat() basics

ObjectKnown before , now let's discuss the following method create(), it can still create objects, but it is not the same as ordinary object creation. Let's briefly analyze:

Obeject.create(xxx); // xxx处可以指定自定义原型或者填写null
function Obj(){
    
    
}
Obj.prototype.num = 1;
var obj1 = Object.create(Obj.prototype);
console.log(obj1);

Printed out, the instance prototype now constructorpoints to this Obj(). Well, then we look at with newinstance objects out, is there any difference?

function Obj(){
    
    
}
Obj.prototype.num = 1;
var obj1 = Object.create(Obj.prototype);
var obj2 = new Obj();
console.log(obj1);
console.log(obj2);

Looking at the results below, there is actually no difference, because they are all Objcreated based on the prototype.

Go ahead and look at the following situation:

var obj1 = Object.create(null);
console.log(obj1);

Through the Object.create(null)created object, there is nothing in it. Here is also a point. Although the top of the prototype chain is Object.prototype, this special empty object does not have a prototype, and it will not inherit from it Object.prototype. Therefore, not all objects inherit Yu Object.prototype.

Note that we can't make our own. After creation __proto__, it's just equivalent to the property value. The instance object has no way to call the method on the prototype object.

var obj = Object.create(null);
obj.num = 1;
var obj1 = {
    
    
  count: 2
}
obj.__proto__ = obj1;
console.log(obj);
console.log(obj1);

Look at this picture below, Youmie have found any different for our self-made __proto__darker there?

Now explore what made their own __proto__can not have access to something on the prototype object.

var obj = Object.create(null);
obj.num = 1;
var obj1 = {
    
    
  count: 2
}
obj.__proto__ = obj1;
console.log(obj.count);

The result is undefinedthat there is obviously no way to visit, which proves that we can't make our own __proto__.

Okay, some objects are created above. Below we will discuss special examples, such as nulland undefined.

There above a conclusion, we found that in addition to open space can inherit the object Object.prototype, and then visit one of the ways toString(), then null, and undefinedyou can do? Let's test it:

console.log(null.toString()); // TypeError: Cannot read property 'toString' of null
console.log(undefined.toString()); // TypeError: Cannot read property 'toString' of undefined

So, why the following code output 1it?

var num = 1;
console.log(num.toString()); // 1

Before explaining, let's review the knowledge points:

The original value of the property is not, why can call the toString()method, the third block is the paper catalog to explain the concept of packaging the class of.

Its working process is as follows:

First new Number(1), then the toString()method is called , so newit became an object before . To be more accurate, let's print it out.

var num = 1;
console.log(num.toString());

var num2 = new Number(num);
console.log(num2);

I found newlater, and in __proto__which did find a toString()method.

Back to the beginning, nulland undefinedwhy not? Because I mentioned above nulland undefinedcan not be packaged. It is always the original value, and there is no prototype or inheritance.

Let's discuss the issues related to implicit conversion and inheritance. The code is as follows:

var num = 1;
var obj = {
    
    };
var obj2 = Object.create(null);
document.write(num);
document.write(obj);
document.write(obj2);

Then we find that the last had a print error: can not be converted to the original value , because ah, obj2empty object is created not inherited Object.prototypeand, therefore, there is no corresponding toString()method. Of course it cannot be converted. ( document.write()Method converted to string)

At last

The output of articles is not easy, and I hope you all support a wave!

Past selections:

Note Warehouse of Little Lion Front End

Visit Chaoyi's blog , which is convenient for friends to read and play~

学如逆水行舟,不进则退

Guess you like

Origin blog.csdn.net/weixin_42429718/article/details/108650077
Recommended