I see that in different plugins and codes, but I don't understand what does that function... In the jQuery api isn't referenced!
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.
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() { });
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With