Detailed usage of jQuery. extend functions _ jquery

Source: Internet
Author: User
JQuery. extend function usage in detail. If you are studying jquery, you can refer to Jquery's extension method. extend is a common method in the process of writing plug-ins. This method has some heavy-load prototypes. Here, let's get to know about it.

The Extension Method prototype of Jquery is:

Extend (dest, src1, src2, src3 ...);
It means to merge src1, src2, src3... into the dest, and the returned value is the merged dest. It can be seen that the structure of the dest is modified after the method is merged. If you want to get the merged result but do not want to modify the dest structure, you can use the following:
Var newSrc = $. extend ({}, src1, src2, src3...) // "{}" is used as the dest parameter.
In this way, you can merge src1, src2, src3... and return the merged result to newSrc. For example:
Var result = $. extend ({}, {name: "Tom", age: 21 },{ name: "Jerry", sex: "Boy "})
Then the merged result
Result = {name: "Jerry", age: 21, sex: "Boy "}
That is to say, if the following parameter has the same name as the preceding parameter, the following parameter will overwrite the preceding parameter value.

You may have some knowledge about this function. Let's look at another official jquery example (http://api.jquery.com/jQuery.extend)
The Code is as follows:

The Code is as follows:





Script



Script
Var defaults = {validate: false, limit: 5, name: "foo "};
Var options = {validate: true, name: "bar "};
/* Merge defaults and options, without modifying defaults */
Var settings = $. extend ({}, defaults, options); // It is often used in Plug-in development.
VarprintObj = typeofJSON! = "Undefined "? JSON. stringify: function (obj ){
Vararr = [];
$. Each (obj, function (key, val ){
Varnext = key + ":";
Next + = $. isPlainObject (val )? PrintObj (val): val;
Arr. push (next );
});
Return "{" + arr. join (",") + "}";
};

$ ("# Log"). append ("

Defaults --"+ PrintObj (defaults) +"

");
$ ("# Log"). append ("

Options --"+ PrintObj (options) +"

");
$ ("# Log"). append ("

Settings --"+ PrintObj (settings) +"

");
Script



Output result:
Defaults -- {"validate": false, "limit": 5, "name": "foo"} // defaults is output as is
Options -- {"validate": true, "name": "bar"} // The Original output options
Settings -- {"validate": true, "limit": 5, "name": "bar"} // merge ults and options, if the following parameters have the same name as the preceding parameters, the following parameters will overwrite the preceding parameter values.

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.