$.extend () Learn about the experience

Source: Internet
Author: User
Tags instance method

2.1 Extend (result,item1,item2 ...)

This method is used primarily for merging, merging all parameter items into result, and returning result, but this

Will destroy the structure of result.

2.2 Extend ({},item1,item2,......)

With this method, you can combine the resulting results in {} and return them without destroying the structure of the original item.

Example:

Var item={name: "Olive", age:23};

Var item1={name: "Momo", Sex: "Gril"};

Var result=$.extend ({},item,item1);

Results:

Result={name: "Momo", Age:23,sex: "Gril"};

Description

The results above show that the Extend method merges all the items into the {}, but it is a little more careful to find that the name in Item1: "Momo" overwrites the name: "Olive" in item, what's going on? Please look down.

2.3 Extend (bool,{},item1,item2 ...)

The Extend method also has overloads with the bool type parameter.

A bool parameter of TRUE indicates a deep copy, and a shallow copy when False. You can use this example to illustrate:

Example:

var item={name: "Olive", Age:23,address{provice: "Henan", City: "Zhengzhou"};

var item1={sex: "Girl", Address{city: "Beijing"};

var result=$.extend (TRUE,ITEM,ITEM1);

var result1=$.extend (FALSE,ITEM,ITEM1);

Results:

Result={name: "Olive", Age:23,sex: "Gril", Address:{provice: "Henan", City: "Beijing"};

Result1={name: "Olive", Age:23,sex: "Gril", address:{City: "Beijing"};

Description

The above results show that when the parameter is ture, which is a deep copy, when the subkey in subkey Item1 has the same value as the subkey in item, the value of the ITEM1 sub-item overrides the value in the Item subkey, and when the child item Item1 property is not the same as the property in item. will be merged with item.

When the argument is false, the subkey in the subkey Item1 is in the same time as the subkey property in item, and the value of the ITEM1 item is completely overwritten by the value in item.

2.4 $.extend (item)

The method is to merge the item into the jquery global object, which is equivalent to adding a

static method (corresponding to the static method here, of course, there is an example method, in the back of the introduction).

$.extend ({

Min:function (A, b) {return a < b a:b;},

Max:function (A, b) {return a > b a:b;}

})

Console.log ($.min (2,3))

$.max (4,5)

Description: This method is equivalent to adding a new method to the jquery class.

Jquery.extend (object); To extend the jquery class itself. Adds a new method to the class.

2.5 $.fn.extend (item)

The $.extend mentioned above adds a static method to the JQuery class, so here's the $.fn.extend (item

) is to add an instance method for each instance.

Example:

$.fn.extend ({hello:function (value) {alert ("Hello" +value);});

After you have written this, you can call the method after you have obtained each example:

$ ("#id"). Hello ("Olive");

Tags: #id can not exist, must have an object

JQuery.fn.extend (object); Adds a method to a jquery object. This should be very well understood.

$.extend () Learn about the experience

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.