Open swiftwind0405 opened 4 years ago
这个主题感觉前前后后花了很久,每次因为有时间没继续的时候,下次再切回来的时候,又要花好多时间精力才能接着进行。 单纯看ES规范文档真的很枯燥,也很容易放弃。我这里的经验是顺腾摸瓜,从对应的某条规范开始看起。必须看英文文档,实在看不懂的地方再去看相应的中文文档。
如果调用GetValue(V)一定不是Reference吗
大佬,这里写的都是 ES5 的规范,那 ES6 里有没有变化呢?
大佬,这里写的都是 ES5 的规范,那 ES6 里有没有变化呢?
老铁,别这样,我也不咋懂。。。据我现在的了解,应该是没有变化吧?如果有见解,可以讨论讨论。
理论
当JavaScript代码执行一段可执行代码(executable code)时,会创建对应的执行上下文(execution context)。
对于每个执行上下文,都有三个重要属性:
规范中之处ECMAScript有三种可执行代码:
其中,对于全局代码直接指向global object,eval代码由于已经不推荐使用暂不做讨论,我们主要关注函数代码中的 this 如何指定。
进入函数代码(ES5-10.4.3)
规范指出,当执行流进入函数代码时,由函数调用者提供 thisArg 和 argumentsList。所以我们需要继续寻找,查看函数调用时候this是如何传递进去的。 (这里只需知道第2点:函数调停者提供的 thisArg 为 null 或 undefined 的时候, this 绑定的是全局对象即可)
函数调用(ES5-11.2.3)
从上述规范中,在函数调用发生时,首先会对函数名部分进行计算并赋值给 ref ,6、7步中几个if else就决定了一个函数调用发生时,this会指向何方。
Reference type(ES5-8.7)
Reference type按字面翻译就是引用类型,但是它并不是我们常说的JavaScript中的引用类型,它是一个规范类型(实际并不存在),也就是说是为了解释规范某些行为而存在的,比如delete、typeof、赋值语句等。规范类型设计用于解析命名绑定的,它由三部分组成:
用大白话讲,就是规范中定义了一种类型叫做Reference用来引用其他变量,它有一个规定的数据结构。由于是规范类型,所以什么情况下会返回Reference规范上也会写得一清二楚。
举例:
实战解析
foo();
foo.bar();
(f = foo.bar)();
foo();
foo.bar();
(f = foo.bar)();
速查表
参考文档