From 69fd1a1e514fc889b4205794071213d9f9aca6c3 Mon Sep 17 00:00:00 2001 From: chan <22977219@qq.com> Date: Wed, 20 Mar 2019 23:11:14 +0800 Subject: [PATCH] feat: edit test.js for learn this --- test/test.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/test.js b/test/test.js index 63a72e4..17f2125 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正是obj + this.should.equal(obj) done() }, 0) } @@ -15,7 +16,7 @@ describe('this', function () { it('global', function () { function test() { // this 是什么?想想为什么? - this.should.equal(null) + this.should.equal(global) } test() }) @@ -26,7 +27,7 @@ describe('this', function () { say: function () { function _say() { // this 是什么?想想为什么? - this.should.equal(null) + this.should.equal(global) } return _say.bind(obj) }() @@ -39,7 +40,7 @@ describe('this', function () { obj.say = function () { function _say() { // this 是什么?想想为什么? - this.should.equal(null) + this.should.equal(obj) } return _say.bind(obj) }()