programmer icon indicating copy to clipboard operation
programmer copied to clipboard

ECMAScript规范

Open Moking1997 opened this issue 4 years ago • 0 comments

ECMAScript规范

11.2.3 函数调用:Function Calls

这里讲了浏览器在进行函数调用时的执行过程:

  1. 计算MemberExpression的结果赋值给ref
  2. funcGetValue(ref)
  3. argList 为解释执行 Arguments 的结果 , 产生参数值们的内部列表 (参考11.2.4).
  4. 如果 Type(func) is not Object ,抛出一个 TypeError 异常 .
  5. 如果 IsCallable(func) is false ,抛出一个 TypeError 异常 .
  6. 如果Type(ref)Reference,那么
    1. 如果 IsPropertyReference(ref)true, 那么 让 this的值为GetBase(ref).
    2. 否则, 当ref的基值baseEnvironment Record时,那么让this的值为ImplicitThisValue(ref).
  7. 否则,Type(ref) 的值不为Reference时,那么this的值为undefined
  8. 返回调用func[call] 内置方法的结果 , 传入 thisValue 作为 this 值和列表 argList 作为参数列表

Reference

The Reference type is used to explain the behaviour of such operators as delete, typeof, and the assignment operators. Reference 类型就是用来解释诸如 delete、typeof 以及赋值等操作行为的。 A Reference is a resolved name binding. 被解析的命名绑定

Reference 由三个组成部分,分别是:

  • base value:为 property 对象或环境记录(environment record)。
  • referenced name:为标识符或属性名
  • strict reference:是否开启了严格模式 iNAxgr
"use strict";

var foo;
// 对应的Reference是:
var fooReference = {
    base: EnvironmentRecord,
    name: 'foo',
    strict: true
};

// or
foo.bar;
// 对应的Reference是:
var barReference = {
    base: foo,
    name: 'bar',
    strict: true
};

// or 使用未声明的变量
a;
varaReference = {
    base: undefined,
    name: 'a',
    strict: true
};

另外需要记住几个方法:

  1. 使用GetBase 获得Referencebase value
  2. 使用IsPropertyReference判断base value是否为一个对象,是就返回true
  3. 8.7.1中,讲了一个用于从 Reference 类型获取对应值的方法: GetValue
var foo =  1;
var fooReference = {
    base: EnvironmentRecord,
    name: 'foo',
    strict: false
};
GetValue(fooReference) // 1;
  1. 10.2.1.1.6ImplicitThisValue()始终返回undefined

实例解释

var value = 1;

var foo = {
  value: 2,
  bar: function () {
    return this.value;
  }
}

//示例1
console.log(foo.bar());
//示例2
console.log((foo.bar)());
//示例3
console.log((foo.bar = foo.bar)());
//示例4
console.log((false || foo.bar)());
//示例5
console.log((foo.bar, foo.bar)());

参考

  1. JavaScript深入之从ECMAScript规范解读this #7
  2. 从 this 指向到 reference 类型
  3. 中文:ECMAScript 5.1 规范地址
  4. 英文:ECMAScript 5.1 规范地址

Moking1997 avatar Feb 28 '21 13:02 Moking1997