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
本文深入探讨JavaScript中两种常见的继承方式:通过原型对象和对象冒充实现继承,重点阐述对象冒充中this对象的动态链接机制及其实现继承的关键步骤。
831

被折叠的 条评论
为什么被折叠?



