From 466473c146a3969e08ea8614b3c03edd97b1425c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9F=AF?= <451119099@qq.com> Date: Fri, 22 Mar 2019 16:20:14 +0800 Subject: [PATCH] update --- test/test.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/test.js b/test/test.js index 63a72e4..26a3231 100644 --- a/test/test.js +++ b/test/test.js @@ -4,7 +4,8 @@ describe('this', function () { say: function () { setTimeout(() => { // this 是什么?想想为什么? - this.should.equal(null) + // 箭头函数由于没有自己的this,所以内部的this就是外层代码块的this,因此这块的this指向最外一层的作用域环境 + this.should.equal(obj) done() }, 0) } @@ -15,7 +16,8 @@ describe('this', function () { it('global', function () { function test() { // this 是什么?想想为什么? - this.should.equal(null) + // this指向为调用它的对象,test()为全局调用函数,如果是nodejs那么全局是global,浏览器在严格模式下为undefined + this.should.equal(undefined) } test() }) @@ -26,7 +28,8 @@ describe('this', function () { say: function () { function _say() { // this 是什么?想想为什么? - this.should.equal(null) + // bind改变this的指向为obj但是obj声明了没有赋值,所以this的指向为undefined + this.should.equal(undefined) } return _say.bind(obj) }() @@ -39,7 +42,8 @@ describe('this', function () { obj.say = function () { function _say() { // this 是什么?想想为什么? - this.should.equal(null) + // bind修改了this的指向为obj + this.should.equal(obj) } return _say.bind(obj) }()