来自 MDN:https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this ,它说:
然而,在严格模式下, this 的值保持在进入执行上下文时设置的任何值,因此,在以下情况下, this 将默认为 undefined:
function f2() {
'use strict'; // see strict mode
return this;
}
f2() === undefined; // true
这表明如果我(1)“使用严格”;(2) 在另一个函数中定义 f2,调用 f2 会为 f2 绑定外部函数的 this。但!
它不起作用...
'use strict';
function AlarmClock(clockName) {
this.clockName=clockName;
}
console.log("hello, let's see some weird stuff");
console.log("THIS in global", this);
AlarmClock.prototype.start = function(seconds) {
console.log('outer', this);
var testInside = function() {
console.log('inner', this);
}
testInside();
}
var clock = new AlarmClock("Horizons")
clock.start(1);
// WITHOUT CONSTRUCTORS
function withoutOut() {
console.log("withoutOut", this)
this.howard="notSoBad";
console.log("modifiedTheThis, should have Howard", this)
function withoutIn1() {
console.log("withoutIn1", this);
console.log("Strict should set to the defining object's this");
}
var withoutIn2 = function() {
console.log("withoutIn2", this);
console.log("Strict should set to the defining object's this");
}
withoutIn1();
withoutIn2();
}
withoutOut.bind({Moose: "genius"})();
console.log("DONE");
给出这个输出:
hello, let's see some weird stuff
THIS in global {}
outer AlarmClock { clockName: 'Horizons' }
inner undefined
withoutOut { Moose: 'genius' }
modifiedTheThis, should have Howard { Moose: 'genius', howard: 'notSoBad' }
withoutIn1 undefined
Strict should set to the defining object's this
withoutIn2 undefined
Strict should set to the defining object's this
DONE
注意:我从 mac osx 命令行使用 node v10.5.0 运行它。
注意 2:如果您在 devtools 中运行,则必须按照以下步骤使用 strict。
NOTE3:基本上,我想找到某种方法来获得内部,withoutIn1 或 withoutIn2 不会被定义。而且,我知道您可以通过显式绑定来执行此操作,但我想专门获取 MDN 文档中指定的行为。
如果您同意我的观点,MDN 应该将“this”文档更改为在“use strict”的函数上下文中,将其始终设置为未定义。(作者喜欢)
或者,Chrome 应该更改实现。(作者不喜欢)