Javascript 什么是 .apply jQuery 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3796788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:12:34  来源:igfitidea点击:

What's the .apply jQuery function?

javascriptjquerypluginsapply

提问by CRISHK Corporation

I see that in different plugins and codes, but I don't understand what does that function... In the jQuery api isn't referenced!

我在不同的插件和代码中看到了这一点,但我不明白那个功能是什么......在 jQuery api 中没有被引用!

回答by Amber

applycalls a function with a set of arguments. It's not part of jQuery, it's part of core Javascript. However, there is mention of it in the jQuery docs:

apply调用带有一组参数的函数。它不是 jQuery 的一部分,而是核心 Javascript 的一部分。但是,在 jQuery 文档中提到了它:

http://docs.jquery.com/Types#Context.2C_Call_and_Apply

http://docs.jquery.com/Types#Context.2C_Call_and_Apply

Syntax:

句法:

somefunction.apply(thisObj, [argsArray])

The above calls the function somefunction, setting thisto thisObjwithin the function's scope, and passing in the arguments from argsArrayas the arguments to the function.

以上调用 function somefunction,在函数的作用域内设置thisto thisObj,并将参数 fromargsArray作为参数传递给函数。

回答by issa marie tseng

Essentially, apply will call a function with the context being set to the object you apply the function to. This means that within the function, referencing thiswill refer to that object.

本质上,apply 将调用一个函数,并将上下文设置为您应用该函数的对象。这意味着在函数内,引用this将引用该对象。