【JavaScript】从 this 指向到 reference 类型
判断“this 指向谁”是个老大难的问题。
网络上有许多文章教我们如何判别,但大多艰涩复杂,难以理解。
那么这里介绍一个非常简单实用的判别规则:
1)在函数【调用】时,“this”总是指向小数点左侧的那个对象
2)如果没有小数点,那么“this”指向全局作用域(比如 Window,严格模式为 undefined)
3)有几个可以改变“this”指向的函数——bind,call 和 apply
4)关键字 “new” 将 “this” 绑定到那个新创建的对象上
好了,我们已经学会了基本的理论知识,是时候运用一波了。请判断下面的 this 指向:
var foo = {bar: function () {return this}}
var b = foo.bar
foo.bar();
b();
var c = new someFunction();
相信你已经完全掌握了,下面我们再进行一点巩固练习:
(f = foo.bar)();
(1, foo.bar)();
(foo.bar)();

并不是上面的判别规则除了差错,而是新增的操作符/运算符增加了代码复杂度。为了解释上述代码的行为,我们需要理解 Reference 和 函数调用。
Reference Specification Type
在 ES5 中,除了基本的 6 种类型(string,number,boolearn,null,undefined,object),还有 reference 类型,不过它对使用者屏蔽,作为开发者,我们也不用关心它。
但是,了解它能够提升我们对 ECMAScript 的认识。
Reference 是什么
ECMAScript 将 Reference 定义为“被解析的命名绑定(resolved name binding)”,它由三部分组成——base,name, and strict flag。
有两种创建 Reference 的途径:
- 标识符解析
- 属性访问
比如,foo 和 foo.bar 创建了一个 Reference ,而字面量(1,“foo”,[1,3]等)或函数表达式——(function(){})却不会。
参考下图:

每创建一个 Reference 都会为其对应的 base,name,strict 设置相应的值。"strict "对应代码是否开启了严格模式;"name"设置为标识符或属性名;"base"设置为 property 对象或环境记录(environment record)。
可以认为 Reference 是一个不带原型、有且只有 3 个属性的对象。譬如说:
'use strict';
var foo;
// 标识符解析会产生 Reference
var Reference = Object.create(null);
Reference.base = EnvironmentRecord;
Reference.name = 'foo';
Reference.strict = true;
// or
foo.bar;
// 属性访问会产生 Reference
var Reference = Object.create(null);
Reference.base = foo;
Reference.name = 'bar';
Reference.strict = true;
// or 使用未声明的变量
a;
var Reference = Object.create(null);
Reference.base = undefined;
Reference.name = 'a';
Reference.strict = true;
函数调用
当函数调用的时候,会发生什么?Function Calls
1. Let ref be the result of evaluating MemberExpression.
2. Let func be GetValue(ref).
3. Let argList be the result of evaluating Arguments, producing an internal list of argument values ([see 11.2.4](https://es5.github.io/#x11.2.4)).
4. If Type(func) is not Object, throw a TypeError exception.
5. If IsCallable(func) is false, throw a TypeError exception.
6. If Type(ref) is Reference, then
* If IsPropertyReference(ref) is true, then Let thisValue be GetBase(ref).
* Else, the base of ref is an Environment Record, Let thisValue be the result of calling the ImplicitThisValue concrete method of GetBase(ref).
7. Else, Type(ref) is not Reference.
* Let thisValue be undefined.
8. Return the result of calling the *Call* internal method on func, providing thisValue as the this value and providing the list argList as the argument values.
ES5 标准告诉我们一个事实——只有在函数真正调用的时候,才能判断 this 的值。
赋值,逗号和分组操作符
有了以上的准备,我们可以解答(f = foo.bar)()、(1, foo.bar)()和(foo.bar)()的 this 指向问题了。
简单赋值(=)操作
诸如 a = 1, g = function(){} 等都属于Simple Assignment,和函数调用一样,在赋值发生之前,JS 也会做一些准备工作:
- Let lref be the result of evaluating LeftHandSideExpression.
- Let rref be the result of evaluating AssignmentExpression.
- Let rval be GetValue(rref).
- Throw a SyntaxError exception if the following conditions are all true:
a. Type(lref) is Reference is true
b. IsStrictReference(lref) is true
c. Type(GetBase(lref)) is Environment Record
d. GetReferencedName(lref) is either "eval" or "arguments"- Call PutValue(lref, rval).
- Return rval.
注意,在赋值前,等号右侧的表达式的值会经过内部函数 GetValue 进行转化。
在我们的例子中,GetValue 将 foo.bar 的引用转化成那个实际的函数。赋值完成后,和调用(function(){})()没有什么分别。现在我们可以使用前面定义的规则来判别 this 指向了,显然这符合第二条规则—— this 指向全局。
逗号操作符
上面的过程也适用于逗号操作符 Comma Operator ( , )
- Let lref be the result of evaluating Expression.
- Call GetValue(lref).
- Let rref be the result of evaluating AssignmentExpression.
- Return GetValue(rref).
GetValue 将 foo.bar 的引用转化成那个实际的函数。逗号操作符计算完成后,和调用(function(){})()没有什么分别。
分组操作符
Grouping Operator会使用 GetValue 计算表达式吗?
Return the result of evaluating Expression. This may be of type Reference.
This algorithm does not apply GetValue to the result of evaluating Expression.
由于分组操作符不会对表达式做额外的操作,所以(foo.bar)() 和 foo.bar()没有差别,this 指向 foo。
(完)
如果你想知道更多细节,不妨点击Annotated ECMAScript 5.1。
虽然不是标准文档,但更容易阅读。
参考
【JavaScript】从 this 指向到 reference 类型的更多相关文章
- javaScript事件(八)事件类型之变动事件
DOM2级的变动(mutation)事件能在DOM中某一部分发送变化时给出提示.变动事件为XML或HTML DOM设计的,并不特定于某种语言.DOM2级定义了如下变动事件. DOMSubtreeMod ...
- 图解javascript中this指向
JavaScript 是一种脚本语言,支持函数式编程.闭包.基于原型的继承等高级功能.JavaScript一开始看起来感觉会很容易入门,但是随着使用的深入,你会发JavaScript其实很难掌握,有些 ...
- JavaScript 中的数字和日期类型
本章节介绍如何掌握Javascript里的数字和日期类型 数字EDIT 在 JavaScript 里面,数字都是双精度浮点类型的 double-precision 64-bit binary form ...
- javascript的this指向
JavaScript 是一种脚本语言,支持函数式编程.闭包.基于原型的继承等高级功能.JavaScript一开始看起来感觉会很容易入门,但是随着使用的深入,你会发现JavaScript其实很难掌握,有 ...
- javaScript事件(六)事件类型之滚轮事件
滚轮事件其实就是一个mousewheel事件,这个事件跟踪鼠标滚轮,类似Mac的触屏版. 一.客户区坐标位置 鼠标事件都是在浏览器视口的特定位置上发生的.这个位置信息保存在事件对象的clientX和c ...
- javascript学习笔记(四) Number 数字类型
数字格式化方法toFixed().toExponential().toPrecision(),三个方法都四舍五入 toFixed() 方法指定小数位个数 toExponential() 方法 用科学 ...
- JavaScript对象的指向问题
JavaScript对象的指向问题 标签(空格分隔): JavaScript 对象 在接触了JavaScript之后,我们常听到一句话就是一切皆对象,意思是说除了object以外,JavaScript ...
- javaScript事件(九)事件类型之触摸与手势事件
一.触摸事件 touchstart:当手指触摸屏幕时触发:即使已经有一个手指放在了屏幕上也会触发. touchmove:当手指在屏幕上滑动时连续地触发.在这个世界发生期间,调用preventDefau ...
- javaScript事件(七)事件类型之键盘与文本事件
键盘事件如下: keydown:当用户按下键盘上的任意键时触发,而且如果按住不放的话,会重复触发此事件. keypress:当用户按下键盘上的字符键时触发,而且如果按住不放的话,会重复触发此事件. k ...
随机推荐
- 【转载】windbg 常用命令详解
windbg 常用命令详解 https://blog.csdn.net/chenyujing1234/article/details/7743460 vertarget 显示当前进程的大致信息 lmv ...
- 发布jar包到远端github仓库使用(将github仓库当作maven仓库)
今天把单点登陆的core模块搬到了github仓库 并且利用github仓库作为maven仓库 在项目中进行了引用 1. 起初看技术博客没有完全引入进来,调整了一下OK了 2. 还可以将其他模块或者工 ...
- luogu P4248 [AHOI2013]差异 SAM
luogu P4248 [AHOI2013]差异 链接 luogu 思路 \(\sum\limits_{1<=i<j<=n}{{len}(T_i)+{len}(T_j)-2*{lcp ...
- 第08组 Alpha冲刺(5/6)
队名:955 组长博客:https://www.cnblogs.com/cclong/p/11898112.html 作业博客:https://edu.cnblogs.com/campus/fzu/S ...
- RAdam VS Adam
论文解读:Radam:ON THE VARIANCE OF THE ADAPTIVE LEARNING RATE AND BEYOND 1,目的 想找到一个比较好的优化器,能够在收敛速度和收敛的效果上 ...
- 【技术博客】基于JsPlumb和JQuery-UI的流程图的保存和再生成
开发组在开发过程中,都不可避免地遇到了一些困难或问题,但都最终想出办法克服了.我们认为这样的经验是有必要记录下来的,因此就有了[技术博客]. 基于JsPlumb和JQuery-UI的流程图的保存和再生 ...
- 阿里云ECS服务器相关配置以及操作---上(初学者)
最近买了一台阿里云的ECS服务器 linux系统 centos镜像,把我相关的一些操作记录下来,供大家参考,不足之处欢迎指正. 首先买的过程就不用介绍了,根据自己的实际需要选择自己想要的配置,点击付钱 ...
- 科研黑帮 | Molecular Genetic Anatomy and Risk Profile of Hirschsprung’s Disease
PNAS又来一篇:Gene- and tissue-level interactions in normal gastrointestinal development and Hirschsprung ...
- Emotion Recognition Using Graph Convolutional Networks
Emotion Recognition Using Graph Convolutional Networks 2019-10-22 09:26:56 This blog is from: https: ...
- mysql 大小写不敏感
lower-case-table-names=1 变量lower-case-table-names的取值 取值范围有三个,分别是0.1.2. 1. 设置成0:表名按你写的SQL大小写存储,大写就大写小 ...