Learn JavaScript through the jquery source code (II)

Source: Internet
Author: User

    1. anonymous functions

(Function (window, undefined) {}) (window)

The reason for passing in the window variable is that the window is changed from a global variable to a local variable, and the scope chain is not returned to the top-level scope for faster access to the window.

Reasons for increasing undefined in the argument list: Make sure that undefined is really undefined in the scope of the self-invocation of the anonymous function.

Benefits:

To create a private namespace, methods and variables within a function body do not affect global space, and do not conflict with other program variables and methods, which are legendary non-polluting global variables.

2. function extension.

(function (window, undefined) {    var jquery = function ()      {        return new jquery.fn.init ();     }    jQuery.fn = jQuery.prototype = {                 version:  ' 1.8.3 ',         init: function ()          {            return this;         }    }     jquery.fn.init.prototype = jquery.fn;    jquery.extend =  Jquery.fn.extend = function (obj) {        for (var n  in obj)             this[n] = obj[n];        return  This;    }    jquery.fn.extend ({         test: function () {             Console.log (' hello world!! ');         }    });     Window.jquery = window.$ = jquery;}) (window)



Learn JavaScript through the jquery source code (II)

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.