简体   繁体   English

无法将“ this”绑定到导入的函数-JavaScript

[英]Can not bind 'this' to imported function - javascript

I am a beginner and I want to bind 'this' to helper-functions which I have imported from an other file (that I can use the variables, I created in the current lexical environment ). 我是一个初学者,我想将'this'绑定到我从另一个文件导入的辅助函数(我可以使用在当前词法环境中创建的变量)。

But I have recognized, that I can bind these imported functions to any object - which I have created in the file, into I have imported them - but not to the current this. 但是我已经认识到,我可以将这些导入的函数绑定到我在文件中创建的任何对象(我在文件中创建的对象)中,但是不能绑定到当前对象。

node.js example: https://github.com/sdeli/issues-with-this node.js示例: https//github.com/sdeli/issues-with-this

 // random-js-file.js // this is the function I import in app.js function logOutThis() { console.log(this); } module.exports = logOutThis; // -------------------------------- // app.js const importedFn = require('./random-js-file.js'); // this is now the global of random-js-file.js importedFn(); console.log('--------------------------------'); var monkey = { chimp : 'chimp' } // this is now the object 'monkey' var myfunction = importedFn.bind(monkey); myfunction(); console.log('---------------------------------'); //this should be now the current this var myfunction2 = importedFn.bind(this); myfunction2(); // console.log displays '{}' and i can not refer to the variable in this lexical environment 

So I dont understand why I can not bind 'this' into a function which I have imported but I can bind it to any object. 所以我不明白为什么我不能将'this'绑定到已导入的函数中,但可以将其绑定到任何对象。

Thank you for your suggestions 谢谢你的建议

There isn't a way to access the inner context of an imported module unless it explicitly exports the bits you want (or provides functions that expose them). 除非它显式导出所需的位(或提供公开这些位的功能),否则无法访问导入的模块的内部上下文。 Javascript this doesn't behave like Java this , and modules aren't classes. 使用Javascript this并不表现像Java this ,和模块并不类。

There's some other cases, but basically, if the function you're currently is called someFunc and it was called with syntax a.someFunc() , then this will be the same as a . 还有其他一些情况,但基本上,如果您当前使用的函数称为someFunc并且使用语法a.someFunc()进行调用,则this a.someFunc()将与a相同。 Here's a worked example: 这是一个可行的示例:

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
}

Basically, IMHO, this is a broken mess in Javascript and is one of the key reasons I avoided using it whereever possible. 恕我直言,基本上, this是Java语言中的一团糟,并且是我避免尽可能使用它的关键原因之一。 That means I don't use classes, and I get everything I need from closures. 那意味着我不使用类,而我从闭包中得到了我需要的一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM