实例结构图
实例代码
function Parent(){
this.neme = 'scl';
}
function Child(){
Parent.call(this);
this.age = '18';
}
Parent.prototype.getname = 'zld'
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
let kid = new Child();
let name = new Parent();
实例结果
实例总结
- 构造函数,自动生成prototype对象,但是实例化对象没有。
- 实例对象可以通过__proto__查找到原型链上的原型对象。
- protype是在new一个实例对象时,构建__proto__对象的。
(new的过程:
①先创建空对象,将构造函数的prototype赋给实例对象的__proto__;
②执行构造函数,更改this指向等;
最后,若构造函数有返回值则返回规定对象,若无则为创建出来的新对象) - __proto__是真正用来查找原型链对象的。
- Object对象的__proto__为null。
- 对于Object.create(),是将所接受的对象放加入到原型链中,并不会将所接收的对象中的属性和方法赋值到本身。
如:
(能获取到a.name是通过原型链上去查找的,而a本身为空)