我是初学者,我想约束这个'我从其他文件导入的辅助函数(我可以使用我在当前词法环境中创建的变量)。
但是我已经认识到,我可以将这些导入的函数绑定到我在文件中创建的任何对象中,然后导入它们 - 但不是当前的。
node.js示例: https://github.com/sdeli/issues-with-this
PostProcessor

所以我不明白为什么我不能约束这个'进入我导入的函数,但我可以将它绑定到任何对象。
感谢您的建议
答案 0 :(得分:0)
除非显式导出所需的位(或提供公开它们的函数),否则无法访问导入模块的内部上下文。 Javascript this
的行为与Java this
不同,模块不是类。
还有一些其他情况,但基本上,如果您当前调用的函数someFunc
并且使用语法a.someFunc()
调用它,则this
将与{{a
相同1}}。这是一个有效的例子:
const someObject = {
someFunc() {
//Here, "this" will be the same as someObject, because of the way we called it
someOtherFunction();
}
};
someObject.someFunc();
function someOtherFunc() {
//"this" is still someObject because it's inherited from the calling function which was not called using a method syntax
x.y(); //inside the y() function, "this" will now be x
}
基本上,恕我直言,this
是Javascript的一个混乱,是我尽可能避免使用它的关键原因之一。这意味着我不使用类,我从闭包中获得了所需的一切。