From d1269e15861a9fd17929c96546e0a047bc379368 Mon Sep 17 00:00:00 2001 From: niduyi52 <906330048@qq.com> Date: Fri, 22 Mar 2019 14:37:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4exercise4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 62 +++++++++++++++++++++++++++++++++++++++++++++++ test/test.js | 12 ++++++--- 2 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..9a93599 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,62 @@ +{ + "name": "exercise4", + "version": "0.0.1", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "should": { + "version": "11.2.1", + "resolved": "https://registry.npmjs.org/should/-/should-11.2.1.tgz", + "integrity": "sha1-kPVRRVUtAc/CAGZuToGKHJZw7aI=", + "dev": true, + "requires": { + "should-equal": "^1.0.0", + "should-format": "^3.0.2", + "should-type": "^1.4.0", + "should-type-adaptors": "^1.0.1", + "should-util": "^1.0.0" + } + }, + "should-equal": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/should-equal/-/should-equal-1.0.1.tgz", + "integrity": "sha1-C26VFvJgGp+wuy3MNpr6HH4gCvc=", + "dev": true, + "requires": { + "should-type": "^1.0.0" + } + }, + "should-format": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/should-format/-/should-format-3.0.3.tgz", + "integrity": "sha1-m/yPdPo5IFxT04w01xcwPidxJPE=", + "dev": true, + "requires": { + "should-type": "^1.3.0", + "should-type-adaptors": "^1.0.1" + } + }, + "should-type": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/should-type/-/should-type-1.4.0.tgz", + "integrity": "sha1-B1bYzoRt/QmEOmlHcZ36DUz/XPM=", + "dev": true + }, + "should-type-adaptors": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/should-type-adaptors/-/should-type-adaptors-1.1.0.tgz", + "integrity": "sha512-JA4hdoLnN+kebEp2Vs8eBe9g7uy0zbRo+RMcU0EsNy+R+k049Ki+N5tT5Jagst2g7EAja+euFuoXFCa8vIklfA==", + "dev": true, + "requires": { + "should-type": "^1.3.0", + "should-util": "^1.0.0" + } + }, + "should-util": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/should-util/-/should-util-1.0.0.tgz", + "integrity": "sha1-yYzaN0qmsZDfi6h8mInCtNtiAGM=", + "dev": true + } + } +} diff --git a/test/test.js b/test/test.js index 63a72e4..0c7b240 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指向say,箭头函数是根据当前的词法作用域来决定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指向window,这个是使用了默认绑定,绑定到了全局对象。node环境中为global + this.should.equal(global) } test() }) @@ -26,7 +28,8 @@ describe('this', function () { say: function () { function _say() { // this 是什么?想想为什么? - this.should.equal(null) + // bind在绑定的对象为undefined的时候,默认是绑定全局对象 + this.should.equal(global) } 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) }()