JQuery executes immediately

Source: Internet
Author: User

;(function($) {//     function() {                //  Plugin Implementation code goes here.           };}) (jQuery);     //

The immediate invocation function expression of jquery is written in three ways:
Notation 1:

(function(window, factory) {Factory (window)} (thisfunction() {  Returnfunction() {  //jquery call }}))  

Notation 2:

var function () {    returnfunction() {    // Execute method     }} var jQuery = Factory ();

Notation 3:

(function(window, undefined) {    varfunction() {}     //  ...    Window.jquery = window.$ = jQuery;}) (window);

As you can see from the code above, this function is initialized automatically, so that it is built only once. Give me a detailed look at the advantages of this notation:

1, window and undefined are to reduce the scope scope of variable lookup. When the window is passed to the inside of the closure, it can be used as a local variable in the closure, and it is obviously faster to find it in the window scope.
2, undefined is the same truth, in fact, this undefined is not a JavaScript data type undefined, but an ordinary variable name. Just because the value is not passed to it, its value is that undefined,undefined is not a reserved word for JavaScript.

var undefined = ' Extron network ';(function(window) {  alert (undefined); // IE8 ' Extron Resource Network '}) (window)

1, class-level plug-in development

The most straightforward understanding of plug-in development at the class level is to add a class method to the jquery class, which can be understood as adding a static method.
The typical example is $. The AJAX () function, which defines the function in the jquery namespace. Plug-in development at the class level can be extended in the following ways

1.1 Add a new global function add a global function, we only need to define as follows:

function () {     alert (' This is a test. This was only a test. '

1.2 Adding multiple global functions adds multiple global functions, which can be defined as follows:

function () {     alert (' This is a test. This was only a test. '  function(param) {     alert (' This function takes a parameter, which is "' + param + '".

Called when it is the same as a function: Jquery.foo (); Jquery.bar (); or $.foo (); $.bar (' Bar ');

1.3 Using Jquery.extend (object);

jquery.extend ({     function() {         alert (' This is a test. This was only a test. '      },     function(param) {         alert (' This function takes a parameter, which is ' + param + ' ". );     

1.4 Using namespaces
Although in the jquery namespace, we prohibit the use of a large number of JavaScript function names and variable names. However, it is still inevitable that some functions or variable names will conflict with other jquery plugins, so we are accustomed to encapsulating some methods into another custom namespace.

Jquery.myplugin = {     foo:function() {         alert (' This is a test. This was only a test. '      },     bar:function(param) {         alert (' This function takes a parameter, which is " ' + param + ' ". ' );     

The function that takes the namespace is still the global function, the method that is used when calling:

$.myplugin.foo (); $.myplugin.bar (

With this technique (using a separate plug-in name), we can avoid collisions of functions within namespaces.

JQuery executes immediately

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.