gpt4 book ai didi

javascript - 调用与调用函数

转载 作者:行者123 更新时间:2023-12-03 01:42:59 26 4
gpt4 key购买 nike

到目前为止,我认为“调用”和“调用”函数的含义是相同的。然而,在 YouTube 教程中,它说要通过调用来调用函数。我的第一个想法是措辞有误,但在 W3Schools 的 page 上在函数调用上,它说:

It is common to use the term "call a function" instead of "invoke a function" ... In this tutorial, we will use invoke, because a JavaScript function can be invoked without being called.

好的,所以有区别。它是什么?

最佳答案

您的引用文本:

It is common to use the term "call a function" instead of "invoke a function" ... In this tutorial, we will use invoke, because a JavaScript function can be invoked without being called.

现在让我重新表述一下:

It is common to use the term "call a function" instead of "invoke a function" ... In this tutorial, we will use the term invoke instead of call, because a JavaScript function can be invoked indirectly like fn.call() and fn.apply() without being called directly like fn().

因此,当我执行 fn() 时,它会被直接调用,而当我像 fn.call() 那样执行时,它会被间接调用,但在这两种情况下,函数正在被调用。否则,我在这里看不出有什么区别,我也可以说我可以通过多种方式调用函数,例如:

fn(); // I'm calling it
fn.call(); // I'm calling it
fn.apply(); // I'm calling it

所以,区别在于语义,但在我看来,两者是可以互换的。顺便说一句,我在上面的问题下写了一条评论,我想把它放在这里,即:

IMO, that's a misleading statement. Maybe there are some indication of call/apply or something else but it's totally confusing.

关于javascript - 调用与调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50884893/

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