```这是一个关键字还是字面意思?

发布于 2025-01-30 10:03:00 字数 171 浏览 3 评论 0原文

为什么JS规格调用this一个关键字而不是文字?

它似乎是字面意思,因为我们可以将其用作价值,或者我缺少什么?

return this

那是什么?

Why JS specs call this a keyword and not a literal?

It more seems to be a literal, because we can use it as a value, or what am I missing?

return this

What is it then?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走干脆点 2025-02-06 10:03:00

这并不重要,但是根据规范是一个关键字:请参见关键字和保留的单词文字。该规格还使用文本中的“关键字this”一词,例如(随机选择一个实例),在摘要 getThisenvironment 操作:

...它使用运行执行上下文的词汇环境确定关键字的绑定...

规范的早期版本为非常明确关于它:

[来自ecmascript 2019语言规范]

11.6.2.1关键字

以下令牌是eCmascript的关键字,不得用作Ecmascript程序中的标识符。

语法

关键字::之一
    等待 break case catch class const在代码> 导出 扩展 最后 for function 如果 import in instanceof new return super switch this 投掷 试试/code> void yart

(我的重点。)

继续您问题:

它似乎是字面意思,因为我们可以将其用作值...

我认为在表达式中使用某些内容与产生价值之间没有任何冲突。 是一个关键字,您可以用来访问当前执行上下文的环境记录的 this -thisting 的值。

rici 在评论中提出了一个很好的观点:他们说他们不考虑this作为字面意思,因为与(Say)truefalse不同,它没有固定的值。文字始终具有相同的值,“ a”始终“ a”true始终true,<代码> null 始终是null等。但是在代码中的不同位置具有不同的值。它的作用实际上更像是const,而不是文字。隐式const在范围的环境对象中设置(如果是箭头函数范围)。

但是再次:这真的没关系。这是一个保留的词,具有特殊的目的和功能,似乎足以使我们不必担心它是关键字还是字面的(或两者)。 :-)

It doesn't really matter, but it's a keyword according to the specification: See Keywords and Reserved Words and Literals. The spec also uses the phrase "keyword this" in the text a fair bit, for instance (picking an instance at random), there's this text in the section on the abstract GetThisEnvironment operation:

...It determines the binding of the keyword this using the LexicalEnvironment of the running execution context...

Earlier versions of the specification were quite explicit about it:

[From The ECMAScript 2019 Language specification]

11.6.2.1 Keywords

The following tokens are ECMAScript keywords and may not be used as Identifiers in ECMAScript programs.

Syntax

Keyword :: one of
   await break case catch class const continue debugger default delete do else export extends finally for function if import in instanceof new return super switch this throw try typeof var void while with yield

(My emphasis.)

Continuing with your question:

It more seems to be a literal, because we can use it as a value...

I don't think there's any conflict between something being a keyword and producing a value when used in an expression. this is a keyword you can use to access the value of the ThisBinding of the environment record for the current execution context (or the nearest one that has one).

rici made a really good point in the comments: They said they don't think of this as a literal because unlike (say) true and false, it doesn't have a fixed value. Literals always have the same value, "a" is always "a", true is always true, null is always null, etc. But this has different values in different places in the code. It really acts more like a const than a literal; an implicit const set in the scope's environment object (or not, in the case of arrow function scope).

But again: It really doesn't matter. It's a reserved word that has a special purpose and function in the language, which seems sufficient that we needn't worry about whether it's a keyword or a literal (or both). :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文