您现在的位置是:首页 > 电脑技术查询 > web开发

js 对象冒充解决方法

编辑:chaxungu时间:2022-10-02 23:17:04分类:web开发

js 对象冒充
有没有无参构造函数的继承呢?
类似
JScript codefunction 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 codefunction a(){ this.show=function(){ alert("a"); }}function b(){ a.call(this)}function ok(){ var myB= new b(); myB.show();}