利用call方法模拟面向对象程序设计中的多层次继承
其实如果熟悉java或其他面向对象程序设计思想,理解javascript的call方法的使用,就能很好理解了。
直接给出代码:
function GrandFather(){
this.g=function(msg){
alert(msg+"\n grandFather: Good Morning,grandson!");
}
this.g2=function(msg){
alert(msg+"\n grandFather: have a good day, grandson!");
}
}
function Father(){
GrandFather.call(this);
this.f=function(msg){
alert(msg+"\n father:you bad boy,haha!");
}
}
function Son(){
Father.call(this);
}
var son = new Son();
son.f("son: hi,Dad!");
son.g("grandson: Morning,grandFather!");
son.g2("grandson: Let me walk with you!");