//对象冒充
function FuncA(){
this.name = 'A';
}function FuncB(){
this.method = FuncA;
this.method();//不就等同于FuncA()么;
delete this.method;
}
var a = new FuncA();
var b = new FuncB();
/*
alert(typeof a);
alert(typeof b);
alert(typeof FuncA);
alert(typeof FuncB);
*/
alert(b.name);this.method()之中究竟做了哪些操作呢?
解决方案 »
- 一个简单的JS面向对象问题。。
- 发布网站后正确,但在IDE中开始执行却显示"Automation 服务器不能创建对象"
- |zyciis| 求jquery选择器写法:class有A,但同时不能有B或C或D
- 如何通过map热点实现鼠标点击出现小图片(类似hint)的效果
- 小弟不懂JS,求一代码
- 关于如何使<textarea>里面的内容按照用户输入的格式显示的问题!
- 如何控制当一框架页面刷新时另一嵌入框架也刷新?
- 关于<script src="......">动态问题
- 请教marquee问题。先谢谢了。
- 如何将history的历史记录清空?
- Ext.window前置问题
- 求高手帮写一句JS代码,请看..
换个例子//对象冒充
function FuncA(){
this.name = 'A';
this.age=30;
}
function FuncB(){
this.method = FuncA;
this.method();//不就等同于FuncA()么;
delete this.method;
this.color ="red";
}
var a = new FuncA();
var b = new FuncB();
console.log(a.name);
console.log(b.name);console.log(a.color);
console.log(b.color);
this.method();就是执行了A的构造函数,然后再删除,可以接着执行本身的构造函数,从而完成继承模拟。
错,是等同于FuncA.call(this);
直接执行FuncA里面的this因为作用域关系与外面的this指向不一致,将无法把name成员添加到当前对象,而会添加到window对象上去
其实可以写得更简单:
function FuncB(){
FuncA.call(this);
}
你这点代码只是完成构造函数成员的继承,还要继承原型链上的成员才算圆满
你的意思就是 就跟嵌套function一样。里面的this跟外层的this不同,必须用一个变量保存外层的this,在传入给内层嵌套的函数使用是吧。