Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the .apply jQuery function?

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

like image 228
CRISHK Corporation Avatar asked Sep 26 '10 06:09

CRISHK Corporation


People also ask

What does .apply do in JS?

With apply , you can write a method once, and then inherit it in another object, without having to rewrite the method for the new object. In general, fn. apply(null, args) is equivalent to fn(... args) with the parameter spread syntax.

What is $( function () in jQuery?

jQuery (a library built on Javascript) has built in functions that generally required the DOM to be fully rendered before being called. The syntax for when this is completed is: $(document). ready(function() { });

What will the jQuery code $( P Hide () do?

jQuery hide() Method The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page).

What is the difference between call () and apply ()?

The Difference Between call() and apply() The difference is: The call() method takes arguments separately. The apply() method takes arguments as an array. The apply() method is very handy if you want to use an array instead of an argument list.


2 Answers

apply calls 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:

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

Syntax:

somefunction.apply(thisObj, [argsArray]) 

The above calls the function somefunction, setting this to thisObj within the function's scope, and passing in the arguments from argsArray as the arguments to the function.

like image 60
Amber Avatar answered Oct 05 '22 21:10

Amber


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 this will refer to that object.

like image 23
issa marie tseng Avatar answered Oct 05 '22 23:10

issa marie tseng