JS 8-5 OOP 实现继承的方式

function Person(){}
function Student(){}
Student.prototype = Person.prototype;//此继承方式是错误的,当我们改变Student.prototype的属性时,Person.prototype的属性也跟着改变了。


Student.prototype = new Person();
Student.prototype.__proto__ == Person.prototype;// true
//可以实现继承

Student.prototype = Object.create(Person.prototype)
Student.prototype.__proto__ == Person.prototype // true
//可实现继承 推荐

有些函数没有prototype属性:

function abc(){}

var a = abc.bind(null);

abc.prototype//undefined

有些对象没有原型:

var obj2 = Object.create(null)
obj2.__proto__ //undefined
obj2的原型指向null的prototype属性,所以为undefined

原文地址:https://www.cnblogs.com/chrisghb8812/p/9634211.html