【发布时间】:2012-11-07 13:47:32
【问题描述】:
可能重复:
Crockford’s Prototypal inheritance - Issues with nested objects
我在从原型 A 获取以下代码以执行原型 B 中的函数时遇到问题,想知道是否有任何简单的解决方案:
var Ob = function () {
test = 'hi';
}
Ob.prototype.A = {
that : this,
root : this,
goB : function () {
var that = this;
console.log('hello');
that.B.wtf();
}
}
Ob.prototype.B = {
that : this,
root : this,
wtf : function () {
var that = this;
console.log(that);
}
}
test = new Ob;
test.A.goB();
【问题讨论】:
-
that = this不引用您的Ob实例,而只是您在Ob.prototype上的属性中放置的对象!
标签: javascript oop inheritance prototype