"With a regular function 'this' represents the object that calls the function" 是什么意思?
What does "With a regular function 'this' represents the object that calls the function" mean?
在箭头函数的解释中,w3schools说,"In regular functions the this keyword represented the object that called the function, which could be the window, the document, a button or whatever"和"With a regular function this represents the object that calls the function"。 Link: https://www.w3schools.com/js/js_arrow_function.asp
我试图理解这句话,不是关于箭头函数,而是它本身。似乎我在网上找到的许多其他来源与此相矛盾,说当一个函数被自己调用时(而不是作为一种方法),'this' 被绑定到顶层对象(例如 window) 或未定义,具体取决于严格模式。
所以我构建了一个简单的示例(运行 在浏览器中是非严格的)。我不想使用 w3schools 网站上的示例,因为它们使用事件处理程序进行解释,而且我想确保避免事件处理程序引入的任何其他绑定逻辑。
<script type="text/javascript">
function foo() {
console.log('foo on ' + this.prop);
}
function bar() {
console.log('bar on ' + this.prop);
var f = this.foo;
f();
}
var x = {
prop: 'x'
};
window.prop = 'w';
x.foo = foo;
x.bar = bar;
x.bar();
</script>
此日志:"bar on x" 和 "foo on w",这似乎表明在 foo 中,this 指的是 window。所以很明显,w3schools 的解释并不是指在 bar 中调用 foo 时自动将 foo 的 this 绑定到 bar 的 this。但它还有什么意思?
好吧,让我们从 W3School 名声不佳的前提开始,无论他们在写什么,您都不应该考虑太多。事实上,您最好改为阅读 MDN。
In regular functions the this keyword represented the object that called the function...
嗯,是的,不……一个对象不调用任何东西。一个对象就是。它是其他东西的容器。其中一个 "stuff" 可能是一个函数。然后你可能有 code 从 "container" 中获取函数并调用它。因此,代码调用函数,无论这些函数是 属性 对象还是其他。
如何在该函数内部确定 this
完全取决于 该代码如何调用该函数。简而言之,在表达式 a.b()
中,b
中的 this
将是 a
。作为一般规则,函数内部的 this
将是调用时 .
之前的任何内容。
如果调用前没有.
,例如只有 b()
,没有 this
(具体如何表现取决于严格模式)。该函数是否最初是对象的一部分并不重要;重要的是用来调用它的确切表达式。
有关所有血腥细节,您可以查看 How does the “this” keyword work?。
此处的脚注是您可以 bind
this
on a function or pass another this
explicitly using call
or apply
,而粗箭头函数根本不遵循此规则。
在箭头函数的解释中,w3schools说,"In regular functions the this keyword represented the object that called the function, which could be the window, the document, a button or whatever"和"With a regular function this represents the object that calls the function"。 Link: https://www.w3schools.com/js/js_arrow_function.asp
我试图理解这句话,不是关于箭头函数,而是它本身。似乎我在网上找到的许多其他来源与此相矛盾,说当一个函数被自己调用时(而不是作为一种方法),'this' 被绑定到顶层对象(例如 window) 或未定义,具体取决于严格模式。
所以我构建了一个简单的示例(运行 在浏览器中是非严格的)。我不想使用 w3schools 网站上的示例,因为它们使用事件处理程序进行解释,而且我想确保避免事件处理程序引入的任何其他绑定逻辑。
<script type="text/javascript">
function foo() {
console.log('foo on ' + this.prop);
}
function bar() {
console.log('bar on ' + this.prop);
var f = this.foo;
f();
}
var x = {
prop: 'x'
};
window.prop = 'w';
x.foo = foo;
x.bar = bar;
x.bar();
</script>
此日志:"bar on x" 和 "foo on w",这似乎表明在 foo 中,this 指的是 window。所以很明显,w3schools 的解释并不是指在 bar 中调用 foo 时自动将 foo 的 this 绑定到 bar 的 this。但它还有什么意思?
好吧,让我们从 W3School 名声不佳的前提开始,无论他们在写什么,您都不应该考虑太多。事实上,您最好改为阅读 MDN。
In regular functions the this keyword represented the object that called the function...
嗯,是的,不……一个对象不调用任何东西。一个对象就是。它是其他东西的容器。其中一个 "stuff" 可能是一个函数。然后你可能有 code 从 "container" 中获取函数并调用它。因此,代码调用函数,无论这些函数是 属性 对象还是其他。
如何在该函数内部确定 this
完全取决于 该代码如何调用该函数。简而言之,在表达式 a.b()
中,b
中的 this
将是 a
。作为一般规则,函数内部的 this
将是调用时 .
之前的任何内容。
如果调用前没有.
,例如只有 b()
,没有 this
(具体如何表现取决于严格模式)。该函数是否最初是对象的一部分并不重要;重要的是用来调用它的确切表达式。
有关所有血腥细节,您可以查看 How does the “this” keyword work?。
此处的脚注是您可以 bind
this
on a function or pass another this
explicitly using call
or apply
,而粗箭头函数根本不遵循此规则。