[英]Javascript: Call object function from within a function object
我的問題似乎是,如果我從函數對象中調用它,則我的對象函數不可見。 示例代碼:
function foo()
{
this.bar = function()
{
alert("hit me!");
}
this.sna = {
fu: function ()
{
this.bar();
}
};
}
this
似乎是指sna
而不是foo
。 我如何處理foo
? this.parent
不起作用。
一種選擇是添加this
的引用:
function foo() {
var _t = this;
this.bar = function() { };
this.child = {
this.foo = function() {
_t.bar():
};
};
}
使用變量來引用this
(Foo)。 看到這個-JavaScript | MDN
function Foo() { this.bar = function() { console.log("hit me!"); }; var that = this; this.sna = { fu: function() { that.bar(); } }; } var foo = new Foo(); foo.bar(); foo.sna.fu();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.