```这是一个关键字还是字面意思?
为什么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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不重要,但是根据规范是一个关键字:请参见关键字和保留的单词和文字。该规格还使用文本中的“关键字
this
”一词,例如(随机选择一个实例),在摘要 getThisenvironment 操作:规范的早期版本为非常明确关于它:
(我的重点。)
继续您问题:
我认为在表达式中使用某些内容与产生价值之间没有任何冲突。
此
是一个关键字,您可以用来访问当前执行上下文的环境记录的 this -thisting 的值。rici 在评论中提出了一个很好的观点:他们说他们不考虑
this
作为字面意思,因为与(Say)true
和false
不同,它没有固定的值。文字始终具有相同的值,“ 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:Earlier versions of the specification were quite explicit about it:
(My emphasis.)
Continuing with your question:
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
andfalse
, it doesn't have a fixed value. Literals always have the same value,"a"
is always"a"
,true
is alwaystrue
,null
is alwaysnull
, etc. Butthis
has different values in different places in the code. It really acts more like aconst
than a literal; an implicitconst
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). :-)