继承
function Person(){};
function Student(){};
继承方式:
Student.prototype = Person.prototype; //一、这样会给Student.prototype添加修改的时候,影响Person
Student.prototype = new Person(); //二、构造器构造Person的实例,Person的实例指向Person.prototype,实现继承。缺点:如果Person(name,age) 带参数,这样继承传参存在问题。
Student.prototype = Object.create(Person.prototype); //三、Object.create():创建一空对象,原型指向他的参数。实现继承。
Object.create工作原理 →
Object.create = function ( proto ){
function F (){};
F.prototype = proto;
reuturn new F();//new F() 创建一个对象,对象的原型指向构造器的prototype
}
链式调用
function newClass(){};
newClass.prototype.addClass = function (str){
console.log('Class:'+str);
return this; //这里的this指newClass()这个构造器
}
var test = new newClass();
test.addClass('classA')