读书人

js 对象冒充解决方法

发布时间: 2012-02-05 12:07:14 作者: rapoo

js 对象冒充
有没有无参构造函数的继承呢?
类似

JScript code
function a(){    this.show=function(){        alert("a");    }}function b(){    this.myA=a;    delete this.myA;}function ok(){        var myB= new b();    myB.show();}

这段代码是我自己写的不知道为什么不能运行,求大师指点!

[解决办法]
function b(){
this.myA=a;
delete this.myA;
}
你都没将a中的属性拷贝到b中
this.myA();

其实这里是模拟了call的操作。。。我觉得直接用call会比较直观点

JScript code
function a(){    this.show=function(){        alert("a");    }}function b(){   a.call(this)}function ok(){        var myB= new b();    myB.show();} 

读书人网 >JavaScript

热点推荐