简体   繁体   English

在正常功能和箭头功能的上下文中对此有疑问

[英]Questions about this in the context of normal functions and arrow functions

never fully understood lexical scoping in context of arrow functions and normal functions 从来没有完全理解箭头功能和法线功能的词汇作用域

 this.thisGlobalVariable = 200; function normalFunction() { this.thisVariable = 100; } function normalFunctionTwo() { console.log(this.thisVariable); // 100 console.log(this.globalVariable); // undefined } let anonymousArrowFunction = () => { console.log(this.thisVariable); // undefined console.log(this.thisGlobalVariable); // 200 } normalFunctionTwo(); anonymousArrowFunction(); 

so since anonymousArrowFunction is an arrow function, it's lexical (its scope is the context in which it was created in) here, it'd be the global scope as it's able to access this.thisGlobalVariable? 因此,由于anonymousArrowFunction是箭头函数,因此此处是词法(其作用域是在其中创建上下文的上下文),因此它将成为全局作用域,因为它能够访问this.thisGlobalVariable? whereas normalFunction and normalFunctionTwo as normal functions would create its own this, in its own scope? 而normalFunction和normalFunctionTwo作为普通函数会在自己的范围内创建自己的this? how is normalFunctionTwo able to access this.thisVariable that was defined in normalFunction? normalFunctionTwo如何访问normalFunction中定义的this.thisVariable?

For the normal functions, the value of this is determined at the time the function is called. 对于正常功能,的值this在函数被调用的时候被确定。 You're calling the functions without any context, so this is set to the window object (in non-strict mode). 您在没有任何上下文的情况下调用函数,因此this其设置为window对象(在非严格模式下)。 Since the functions are all interacting with the same window object, they can see eachother's handywork (though you have some bugs in your sample code which are making it not work correctly) 由于这些功能都与同一个窗口对象进行交互,因此它们可以看到彼此的帮助(尽管示例代码中存在一些错误,这些错误使其无法正常工作)

There are other ways the function might be invoked, and they will result in different values of this. 还有其他方法可以调用该函数,它们将导致不同的值。 For example, suppose i stick a function in an object and call it by referencing that object: 例如,假设我将一个函数粘贴在一个对象中并通过引用该对象来调用它:

 this.val = 'from window'; function doStuff() { console.log(this.val); } const myObj = { val: 'from object', log: doStuff } console.log(doStuff === myObj.log) // <-- they're literally the same function doStuff(); // <-- this is set to the window object myObj.log(); // <-- this is set to myObj 

"normal functions" determine their context by how they were called, so: “正常功能”通过调用方式来确定其上下文,因此:

 context.method();

calls method with this pointing to context . 调用methodthis指向context Now you do 你现在做

 normalFunctionTwo();

that calls the function without any context, and then the context defaults to the window object. 会在没有任何上下文的情况下调用该函数,然后上下文默认为window对象。 So this inside normalFunctionTwo (and also outside of it) points to window . 因此, this normalFunctionTwo内部(以及外部)指向window There is no difference to arrow functions in this case. 在这种情况下,箭头功能没有区别。

You're misunderstanding something about how this is being determined: quote from MDN 你误解有关如何东西this被确定:引自MDN

In most cases, the value of this is determined by how a function is called . 在大多数情况下,此值取决于函数的调用方式 It can't be set by assignment during execution, and it may be different each time the function is called. 在执行过程中不能通过赋值来设置它,并且每次调用该函数时可能会有所不同。 ES5 introduced the bind method to set the value of a function's this regardless of how it's called, and ES2015 introduced arrow functions which don't provide their own this binding (it retains the this value of the enclosing lexical context). ES5引入了bind方法来设置函数this的值,而不管其调用方式如何,ES2015引入了arrow函数,它们不提供自己的this绑定(它保留了封闭词法上下文的this值)。

The value of this is not being created by a function and you cannot directly assign to it. this没有被创建的function ,你不能直接分配给它。 Instead this is being resolved before execution (see here ). 相反, this是在执行之前解决的(请参见此处 )。 This is why Function.apply , Function.call and Function.bind exist which may be used to explicitely set this . 这就是为什么可以使用Function.applyFunction.callFunction.bind来显式设置this

The difference between a "normal" function declaration and an arrow function declaration is that in the second this is automatically bound to the enclosing lexical context. “正常”的函数声明和箭头函数声明之间的差别在于,在所述第二this自动绑定到封闭词法上下文。


In your code snippet btw, this always references the global scope. 在您的代码片段btw中, this始终引用全局范围。

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

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