Thursday 29 March 2018

javascript - What does (function($) {})(jQuery); mean?

Firstly, a code block that looks like (function(){})() is merely a function that is executed in place. Let's break it down a little.



1. (
2. function(){}
3. )
4. ()



Line 2 is a plain function, wrapped in parenthesis to tell the runtime to return the function to the parent scope, once it's returned the function is executed using line 4, maybe reading through these steps will help



1. function(){ .. }
2. (1)
3. 2()


You can see that 1 is the declaration, 2 is returning the function and 3 is just executing the function.



An example of how it would be used.




(function(doc){

doc.location = '/';

})(document);//This is passed into the function above


As for the other questions about the plugins:




Type 1: This is not a actually a plugin, it's an object passed as a function, as plugins tend to be functions.



Type 2: This is again not a plugin as it does not extend the $.fn object. It's just an extenstion of the jQuery core, although the outcome is the same. This is if you want to add traversing functions such as toArray and so on.



Type 3: This is the best method to add a plugin, the extended prototype of jQuery takes an object holding your plugin name and function and adds it to the plugin library for you.

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...