Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS:;这";在严格模式下的函数上下文中,MDN规范不';t匹配Chrome67实现_Javascript_Function_This_Executioncontext - Fatal编程技术网

Javascript JS:;这";在严格模式下的函数上下文中,MDN规范不';t匹配Chrome67实现

Javascript JS:;这";在严格模式下的函数上下文中,MDN规范不';t匹配Chrome67实现,javascript,function,this,executioncontext,Javascript,Function,This,Executioncontext,从MDN:,它说: 但是,在严格模式下,该值保持在输入执行上下文时设置的值,因此,在以下情况下,该值将默认为未定义: function f2() { 'use strict'; // see strict mode return this; } f2() === undefined; // true 这表明,如果我(1)“使用strict”;和(2)在另一个函数内定义f2,调用f2将绑定外部函数的this for f2。 但是 它不起作用 “严格使用”; 功能报警时

从MDN:,它说:

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

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

f2() === undefined; // true

这表明,如果我(1)“使用strict”;和(2)在另一个函数内定义f2,调用f2将绑定外部函数的this for f2。 但是

它不起作用

“严格使用”;
功能报警时钟(时钟名称){
this.clockName=clockName;
}
log(“你好,让我们看看一些奇怪的东西”);
log(“这在全局中”,这在全局中);
AlarmClock.prototype.start=功能(秒){
console.log('outer',this);
var testInside=函数(){
log('internal',this);
}
睾丸素();
}
var时钟=新的报警时钟(“地平线”)
时钟启动(1);
//没有构造器
函数without(){
log(“without”,this)
这个。howard=“notSoBad”;
log(“modifiedTheThis,应该有Howard”,this)
函数withoutIn1(){
console.log(“withoutIn1”,this);
log(“Strict应该设置为定义对象的this”);
}
var withoutIn2=函数(){
console.log(“withoutIn2”,this);
log(“Strict应该设置为定义对象的this”);
}
withoutIn1();
withoutIn2();
}
bind({Moose:genius})();

控制台日志(“完成”)
调用函数时,
this
的值与函数的定义方式或位置无关。它只与函数的调用方式有关。调用内部函数:

AlarmClock.prototype.start = function(seconds) {
  console.log('outer', this);
  var testInside = function() {
    console.log('inner', this);
  }
  testInside();
}
testInside()
一样,没有任何对象引用意味着没有任何东西可以绑定到
这个
,在严格模式下,这意味着
这个
未定义的
。然而,如果你已经写了

  testInside.call(this);
然后会有一个上下文值。一个函数“在”另一个函数中,这一事实同样对该函数的绑定方式没有影响。或者:

  this.testInside = testInside;
  this.testInside();
它也会起作用,因为这里又有一个上下文

哦,还有,新的(ish)arrow函数机制确实允许您创建有效地从词法环境中“继承”
这个
值的函数。因此:

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

这样就行了,因为调用arrow函数不涉及任何
绑定<代码>此
的行为本质上类似于闭包中的变量。

您确实误解了
的工作原理。在没有显式上下文的任何函数调用中(没有对象引用,没有使用
.call()
.apply()
,或
.bind()
),严格模式下的
this
的值实际上是
未定义的
。但是,在严格模式下,当存在调用上下文时,此
的工作方式与在非严格模式下的工作方式完全相同。这是说,如果你把它定义为一个对象的方法,
this
的值就是这个对象。它完全没有提到在另一个函数中定义函数。您链接的MDN文档的第二段非常清楚地说明了这一点。这篇文章的措辞令人困惑,但“它的价值仍然是在进入执行上下文时设置的任何值”表示与调用方式相比,
值未发生更改(例如,作为
f()
/
f.call(null)
/
f.call(未定义)
/
f.call(5)
,这将导致
值在严格模式下为
未定义
/
null
/
未定义(全局)
(全局)
(全局)
,以及
新号码(5)
外部–与呼叫相比,这些值已更改)是的,再次阅读引用的段落,我认为这要么是笨拙的词语选择,要么就是完全错误;
this
的值与它在调用环境中的值没有任何关系。(当然箭头函数不同,但是绑定值涉及定义环境。)