Extend usage in extjs developed by jquery plug-in

Source: Internet
Author: User

In jquery, extend is actually used in many plug-ins. Today, we also summarize jquery and ext js.
Extend usage, first look at jquery.
1) extend (dest, src1, src2, src3 ...);
Copy codeThe Code is as follows: var start = {
Id: 123,
Count: 41,
Desc: 'This is information ',
Title: 'base object ',
Tag: 'uncategorized ',
Values: [, 13]};
Var more = {name: 'Los Techies ', tag: 'javascript '};
Var extra = {count: 42, title: null, desc: undefined, values: [1, 3, 6, 10]};
Var extended = $. extend (start, more, extra );
Console. log (JSON. stringify (extended ));

Output result:
{"Id": 123,
"Count": 42,
"Desc": "this is information ",
"Title": null,
"Tag": "javascript ",
"Values": [1, 3, 6, 10],
"Name": "Los Techies "}
As you can see, it is actually
Extend (dest, src1, src2, src3 ...);
, Merge src1, src2, src3... into dest, and return the combined dest. It can be seen that the structure of 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.
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.
In the first example, "desc": undefined does not appear in the result,
In combination, the original value of desc is retained. However, title: null will appear in the extend result.
.
2) usage of other jquery extend
1. $. extend (src)
This method combines src into jquery's global object, for example:
$. Extend ({hello: function () {alert ('hello ');}});
Is to merge the hello method into the global object of jquery.
2. $. fn. extend (src)
This method combines src into jquery's instance object, for example:
$. Fn. extend ({hello: function () {alert ('hello ');}});
Is to merge the hello method into the jquery instance object.
Below are some examples of common extension instances:
$. Extend ({net :{}});
This is to expand a net namespace in the jquery global object.
$. Extend ($. net, {hello: function () {alert ('hello ');}})
This is to extend the hello Method to the expanded Jquery net namespace.
3. Deep Replication
// Previous. extend ()
JQuery. extend (false,
{Name: "John", location: {city: "Boston "}},
{Last: "Resig", location: {state: "MA "}}
);
// Result:
// =>{ Name: "John", last: "Resig", location: {state: "MA "}}
JQuery. extend (true,
{Name: "John", location: {city: "Boston "}},
{Last: "Resig", location: {state: "MA "}}
);
// Result
// => {Name: "John", last: "Resig ",
// Location: {city: "Boston", state: "MA "}}
3) If it is ext js, let's take a look at the differences.:Copy codeThe Code is as follows: var start = {
Id: 123,
Count: 41,
Desc: 'This is information ',
Title: 'base object ',
Tag: 'uncategorized ',
Values: [, 13]};
Var more = {name: 'Los Techies ', tag: 'javascript '};
Var extra = {count: 42, title: null, desc: undefined,
Values: [1, 3, 6, 10]};
Var extended = Ext. apply (start, more, extra); console. log (JSON. stringify (extended ));

Output:
{"Id": 123, "count": 42, "title": null, "tag": "javascript", "values": [,], "name": "Los Techies "}
We can see that extjs uses apply, and desc is actually lost in the combination result, because ext js thinks that undefind should not appear in the combination result, consider that the original value is erased.

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.