function ClassA(sColor){
this.color=sColor;
this.sayColor=function(){alert(this.color);};
}
function ClassB(sColor,sName){}
使用如下构造函数ClassB可以实现继承机制:
function ClassB{
this.newMethod=ClassA;
this.newMethod(sColor);
delete this.newMethod; this.name=sName;//所有的新属性和新方法都必须在删除了新方法的代码行后定义。否则
this.sayName=function(){alert(this.name);};//可能会覆盖超类的相关属性和方法。
}
问题:
1.如果执行了delete this.newMethod;指针ClassA被删除,那么newMethod的属性和方法不就没有指针了么?
2.没有指针的话以后还怎么调用?
this.color=sColor;
this.sayColor=function(){alert(this.color);};
}
function ClassB(sColor,sName){}
使用如下构造函数ClassB可以实现继承机制:
function ClassB{
this.newMethod=ClassA;
this.newMethod(sColor);
delete this.newMethod; this.name=sName;//所有的新属性和新方法都必须在删除了新方法的代码行后定义。否则
this.sayName=function(){alert(this.name);};//可能会覆盖超类的相关属性和方法。
}
问题:
1.如果执行了delete this.newMethod;指针ClassA被删除,那么newMethod的属性和方法不就没有指针了么?
2.没有指针的话以后还怎么调用?
function ClassB{
this.newMethod=ClassA; //this.newMethod ClassA的引用
this.newMethod(sColor); // this,始终指向到调用它的对象 function ClassA(sColor){this.color=sColor -- 这里this指向ClassB.......}
delete this.newMethod; //销毁this.newMethodthis.name=sName;//所有的新属性和新方法都必须在删除了新方法的代码行后定义。否则
this.sayName=function(){alert(this.name);};//可能会覆盖超类的相关属性和方法。
}
这里的this指向的是ClassA对象吧?
function ClassB(sColor){
this.newMethod=ClassA;
this.newMethod(sColor); // this,始终指向到调用它的对象 function ClassA(sColor){this.color=sColor -- 这里this指向ClassB的对象b.......}
delete this.newMethod; this.name=sName;//所有的新属性和新方法都必须在删除了新方法的代码行后定义。否则
this.sayName=function(){alert(this.name);};//可能会覆盖超类的相关属性和方法。
}
var b = new ClassB();alert(b);
//所以delete this.newMethod;也仅仅是把new ClassB的newMethod函数删除而已。
this.newMethod(sColor);
delete this.newMethod;完全可以用
ClassA.call(this, sColor);
替换