什么是词法“this"? [英] What is lexical 'this'?

查看:18
本文介绍了什么是词法“this"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我简单介绍一下这个词法?

Could someone please give me a brief introduction to lexical this?

"与函数表达式相比,箭头函数表达式(也称为胖箭头函数)的语法更短,并且在词法上绑定了 this 值(不绑定自己的 this、arguments、super 或 new.target).箭头函数总是匿名的."

"An arrow function expression (also known as fat arrow function) has a shorter syntax compared to function expressions and lexically binds the this value (does not bind its own this, arguments, super, or new.target). Arrow functions are always anonymous."

这是否意味着当我使用来自Fat Arrow"函数的this"引用调用函数成员时,this"总是指封闭的this"?

Does it mean that when I call a function member using the 'this' ref from a 'Fat Arrow' Function the 'this' always refers to the enclosing 'this'?

推荐答案

您似乎对箭头函数中的 this 会发生什么有正确的理解.我将提供一个我认为有助于对话的解释,并希望能巩固您的理解.

You seem to have the correct understanding of what happens with this in an arrow function. I will offer an explanation that I think adds to the conversation and hopefully solidifies your understanding.

您可能知道,当您定义一个函数并在其中使用一个变量时,它会检查该变量是否已在其作用域中定义.如果是,它会使用它!如果不是,它会检查该变量定义的封闭范围.它不断检查封闭作用域,直到找到变量或到达全局作用域.现在, 箭头函数的函数定义为您隐式地定义了this.因此,当您尝试在其作用域中使用 this 时,它们永远不会检查封闭作用域(因为它们在自己的作用域中找到它!).箭头函数不定义它们自己的 this,因此它们会转到封闭的作用域并查找它,就像它们在您尝试在其作用域中使用的任何变量一样.

As you probably know, when you define a function and use a variable inside of it, it checks if the variable has been defined in its scope. If it is, it uses it! If not, it checks the enclosing scope for that variable definition. It keeps checking enclosing scopes until it finds the variable or reaches global scope. Now, function definitions that are not arrow functions define this for you, implicitly. Thus, they will never check an enclosing scope when you try to use this in their scope (because they find it in their own scope!). Arrow functions do NOT define their own this, so they go to the enclosing scope and look for it just as they would with any variable you try to use in their scope.

这篇关于什么是词法“this"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆