Details of the prototype chain js

1. ways to create objects:

//第一种方式:字面量
var o1 ={name:'o1'};
var o2=new Object({name:'o2'});
//第二种方式:通过构造函数
var M=function(name){this.name=name};
var o3=new M('o3')
//第三种方式:Object.create
var p={name:'p'}
var o4=Object.create(p)

Here Insert Picture Description
M.prototype equal prototype object, by constructor associated prototype object constructor
Here Insert Picture Description

_Proto_ instance of an object is equal to the constructor prototype
Here Insert Picture Description
found by way of the prototype object prototype chain, method Prototype shared object is different instances

M.prototype.say=function(){
	console.log('say hi')
}
var o5 =new M('o5')

Here Insert Picture Description
M._proto_ constructor equal Function.prototype: M is the constructor Function, M can also be understood that normal functions Function instantiate the constructor.
Here Insert Picture Description
Constructor and prototype _proto_ instance of an object is referenced by the same address
Here Insert Picture Description
o3 is an example of the constructor is the prototype example of the constructor, the determination is not directly instantiated, use constractor
Here Insert Picture Description
Here Insert Picture Description

var new2=function(func){
    var o=Object.create(func.prototype);
    var k=func(o);
    if(typeof k==='Object'){
        return k
    }else{
        return o
    }
}

Here Insert Picture Description
o4 name attribute is come through the chain itself is a new prototype object p, so the object is empty o4
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_36711388/article/details/90216709