gpt4 book ai didi

javascript - 在 Javascript ES5 和 ES6 中 Function 对象的调用和 apply 方法的上下文中,use strict 如何影响 this 关键字

转载 作者:行者123 更新时间:2023-11-28 18:11:44 26 4
gpt4 key购买 nike

我想知道在 Javascript ES5 和 ES6 中 Function 对象的调用和 apply 方法的上下文中使用严格模式时 this 的概念。一个带有解释的好例子将受到高度赞赏。

最佳答案

如果函数是严格的,则其 this 值不会转换为对象,而是保持原样。对于非严格函数,this 始终转换为对象(当然,除非它是 nullundefined):

function foo() {
console.log(typeof this);
}
function foo_strict() {
"use strict";
console.log(typeof this);
}

foo.call(42);
foo_strict.call(42);

关于javascript - 在 Javascript ES5 和 ES6 中 Function 对象的调用和 apply 方法的上下文中,use strict 如何影响 this 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41456655/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com