我有一个对象,它还有另一个内部对象。如何从内部对象调用父对象?
var test = {
init: function () {
var instance = this;
},
call: function() {
this.stop(); // works
},
stop: function() {
this.parseText(); // works
},
parseText: {
load: function ()
{
this.call(); //*** dont work
instance.call(); // work, but what if i have this instance (same name) on another object, would'nt this conflict it?
}
}
};
我正在使用一个工作正常的实例,但是如果我或某人在另一个对象中编写了一个实例(名称相同)var,会不会发生冲突并覆盖该实例呢?
最佳答案
如果您担心的只是test
的更改,请按照以下步骤操作:
var test = (function() {
var object = {}
object.call = function() {
this.stop(); // works
};
object.stop = function() {
this.parseText(); // apparently works, even though parseText is not a function
};
object.parseText = {
load: function() {
object.call(); // works
}
};
return object;
})();
关于javascript - javascript对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7944335/