function shared() {
return {
lot: function() {
console.log('crawling');
},
pot: function() {
lot();
console.log("crawled");
}
}
}
var share = new shared();
share.pot();
throw
error lot is not a function
如何调用lot方法
我已经有这样的现有函数结构,如何在不更改结构的情况下调用很多方法
最佳答案
this.lot();
您位于对象内部(!= 范围)。您试图获取作用域内的函数,但它不在作用域内。它是同一个对象的一部分,所以使用这个...
Scope:
window -> lot
window -> pot //lot isnt in the scope, just window is
this:
pot -> (this = Object ) this.lot = Object.lot
正如工程师所指出的,您不需要 new,因为它不是构造函数:
share=shared();
share.pot();
关于javascript - 如何在return中调用工厂函数方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41346538/