赞
踩
继承时候,同名方法发生覆盖了,通过在临时私有变量先保存超类的同名方法,然后在子类同名方法中调用即可!
- function A()
- {
- //通过call传递进来的this就是var b=new B()创建的b对象,所以this.sex打印了female
- this.m=function(){alert("A"+this.sex)}
- }
- function B()
- {
- //用局部变量保存超类继承的同名方法
- var m=this.m;
- this.sex="female";
- this.m=function()
- {
- //覆盖方法中调用超类的同名方法时,需要用call或者apply修改执行上下文为this
- m.call(this);
- //this指向var b=new B()创建的b对象!
- alert("B"+this.sex);
- }
- }
- B.prototype=new A();
- B.prototype.constructor=B;
- var b=new B();
- b.m();

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。