JS:在严格模式下的函数上下文中为“ this”,MDN规范与chrome 67实现不匹配

cho*_*man 1 javascript function this executioncontext

从MDN:https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this,它说:

但是,在严格模式下,此值将保持为进入执行上下文时设置的值,因此,在以下情况下,它将默认为未定义:

function f2() {   
  'use strict'; // see strict mode   
  return this; 
}

f2() === undefined; // true
Run Code Online (Sandbox Code Playgroud)

这表明如果我(1)“使用严格”;(2)在另一个函数中定义f2,调用f2将为f2绑定外部函数的this。但!

没用...

function f2() {   
  'use strict'; // see strict mode   
  return this; 
}

f2() === undefined; // true
Run Code Online (Sandbox Code Playgroud)

给出以下输出:

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
Run Code Online (Sandbox Code Playgroud)

注意:我是通过Mac osx命令行在节点v10.5.0上运行它的。

注意2:如果您在devtools中运行,则必须严格遵循以下步骤。

注3:基本上,我想找到某种方式来获取内部,而没有In1或没有In2则不被定义。而且,我知道您可以使用显式绑定来执行此操作,但是我想专门获取MDN文档中指定的行为。

如果您同意我的看法,则MDN应该将“ this”文档更改为仅在函数上下文中使用“ use strict”将其设置为undefined ALWAYS。(作者喜欢)

或者,Chrome应该更改实现。(作者不喜欢)

Poi*_*nty 5

this何时调用函数的值与函数的定义方式或位置完全无关。它仅与函数的调用方式有关。调用内部函数:

AlarmClock.prototype.start = function(seconds) {
  console.log('outer', this);
  var testInside = function() {
    console.log('inner', this);
  }
  testInside();
}
Run Code Online (Sandbox Code Playgroud)

就像testInside()没有任何对象引用意味着没有任何绑定this,在严格模式下意味着thisundefined。但是,如果您写的是

  testInside.call(this);
Run Code Online (Sandbox Code Playgroud)

那么会有一个上下文值。同样,一个函数位于另一个函数内部的事实对this绑定方式绝对没有影响。或者:

  this.testInside = testInside;
  this.testInside();
Run Code Online (Sandbox Code Playgroud)

它也会起作用,因为再次有上下文。

哦,而且还有,新的(ish)箭头函数机制可以让您创建可以有效“继承” this其词法环境值的函数。所以:

AlarmClock.prototype.start = function(seconds) {
  console.log('outer', this);
  var testInside = () => {
    console.log('inner', this);
  }
  testInside();
}
Run Code Online (Sandbox Code Playgroud)

起作用,因为调用箭头函数不涉及任何this绑定;this行为本质上类似于闭包中的变量。