简体   繁体   English

为什么调用函数的“类型”不是JavaScript(ECMA-262 5.1)中的“引用”?

[英]Why is `Type` of calling function not `reference` in `JavaScript(ECMA-262 5.1)`?

My question arose while I was trying to figure out all the details of this keyword. 当我试图弄清楚this关键字的所有详细信息时出现了我的问题。 So far, I understood that how this value is set by reading ECMAScript® Language Specification Section 10.4.3 Entering Function Code and 11.2.3 Function Call . 到目前为止,我了解了如何通过阅读ECMAScript®语言规范第10.4.3节“ 输入功能代码”和“ 11.2.3功能调用 来设置该值。 And while doing playing with some code, my question arose, please see the code below. 在玩一些代码时,我的问题出现了,请参见下面的代码。

"use strict";
function test(){
alert(this); //undefined
};
test();

According to 10.4.3 Step 1, given thisArg is assigned to this value in strict mode . 根据10.4.3步骤1,给定thisArgstrict mode分配给this值。 And according to 11.2.3 Step 7, thisArg is only undefined when Type(ref) is not a reference . 并且根据11.2.3步骤7,仅当Type(ref)不是reference时,thisArg才是未定义的。

So my question is, why is Type(ref) not a reference in this case? 所以我的问题是,为什么在这种情况下Type(ref)不是reference

Step 7 is irrelevant in this case because it is the else after step 6, which says: 在这种情况下,步骤7是不相关的,因为它是步骤6之后的else ,它表示:

6.If Type(ref) is Reference, then 6,如果Type(ref)是Reference,则
a.If IsPropertyReference(ref) is true, then 如果IsPropertyReference(ref)为true,则
i.Let thisValue be GetBase(ref). i。让thisValue为GetBase(ref)。
b.Else, the base of ref is an Environment Record b。否则,ref的基础是环境记录
i.Let thisValue be the result of calling the ImplicitThisValue concrete method of GetBase(ref). i。让thisValue成为调用GetBase(ref)的ImplicitThisValue具体方法的结果。

In your case test is a Reference, and it is not a PropertyReference, so the ImplicitThisValue is used. 在您的案例中, test 一个引用,而不是 PropertyReference,因此使用ImplicitThisValue。 In strict mode, that value is undefined . 在严格模式下,该值是undefined

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

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