js继承常用的2种通过原型对象以及对象冒充实现的。
对象冒充的精髓在于this对象的替换
而javascript是动态执行的。所以写在闭包内的this并不认识外面的function
必须要执行下。才能把里面的this,动态链接到外面的this上。从而实现继承。
function people(name,age){
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
function student(name,age,grade,school){
this.people=people;
this.people(name,age);
delete this.people;
this.grade=grade;
this.school=school;
}
var s=new student("mr.l",25,"classI","TSING");
s.sayHello();
ps:这句话很重要,否则你的子类中会多一个this.people属性。
原型对象继承的时候有现成的方法可以使用
《未完》prototype