The extend of jquery source code

Source: Internet
Author: User

Jquery.extend = JQuery.fn.extend = function () {

Define some variables

if () {} See if it is a deep copy case

if () {} See the parameter is correct

if () {} see if plug-in situation

for () {There may be multiple object conditions

if () {} Prevents circular references, such as $.extend (A, {name:a}); The case of circular references is excluded;

if () {} deep copy

else if () {} shallow copy

}

}

Knowledge Point carding:

1, jquery is a function, jquery () returns an object, the constructor of this object is JQuery.fn.init (), the constructor of the prototype, that is, JQuery.fn.init.prototype = Jquery.fn, and Jquery.fn = Jquery.prototype, so the prototype function of the paparazzi function jQuery.fn.init () is Jquery.prototype, Summary: The object returned after calling JQuery () is actually inherited from Jquery.prototype prototype;

2, jquery Use the same set of source code for the expansion of static and instance methods, jquery uses two kinds of extend form analysis:

Jquery.extend (): Expanding static methods

JQuery.fn.extend (): extended instance method

$.extend (), this points to $, which is the jquery function, add method Aaa:this.aaa, $.AAA ()

$.fn.extend (), this points to $.fn = Jquery.prototype, adding the method Aaa:this.aaa-$ () in the prototype. AAA (), you need to call the AAA method in the instance object

"When writing only one object argument, JQ expands the two forms of the plugin"

$.extend ({//Extension tool method

Aaa:function () {alert (1);},

Bbb:function () {alert (2);}

});

$.AAA (); Pop up 1

$.BBB (); Pop Up 2

$.fn.extend ({//Extended JQ instance method

Aaa:function () {alert (3);},

Bbb:function () {alert (4);}

});

$ (). AAA ();

$ (). BBB ();

"When writing to an object argument, the following objects are extended to the first object"

var a = {}; Extending the properties of object a

$.extend (A, {name: "Hello"}, {age:30});

alert (a); Popup {name: "Hello", age:30}

var a = {}; You can also make deep and shallow copies.

var b = {name: {age:30}};

$.extend (A, b); $.extend () make a shallow copy!

A.name.age = 20;

alert (b.name.age); Pop up 20

$.extend (True, A, b); Add parameter True to tell JQ I want to make a deep copy

A.name.age = 20;

alert (b.name.age); Pop up 30

3. About inheritance

JQ uses copy inheritance, more flexible;

Class inheritance: Using the new constructor for inheritance;

Prototype inheritance:??

The extend of jquery source code

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.