Js-$. extend extension method to make method parameters more flexible

Source: Internet
Author: User

During JS development, we encapsulate complicated third-party plug-ins and publish a very simple method interface. This is a common method for development. In JS, our method parameters usually use JQ's $. extend extension method to implement
Copy codeThe Code is as follows:
Function extend (arr ){
Arr = $. extend ({name: 'zzl', sex: 'male', age: 31}, arr || {});
Alert ("[Name:]" + arr. name + "[Sex:]" + arr. sex + "[Age:]" + arr. age );
}

Page call:
Copy codeThe Code is as follows:
<A href = "#" onclick = "extend (); return false;"> extends </a>:

 
That is, if we do not specify a value for arr, it will output the default value. What will happen if we assign a value to an attribute of the arr object?
Copy codeThe Code is as follows:
<A href = "#" onclick = "extend ({name: 'occupied '}); return false;"> extends </a>

In the above Code, we changed the property of name and the result output is:


That is, it only modifies the value of the specified attribute, and the other values of the object are kept as the original default values.
| Operator in JS is the same as | in C #. It uses logic or. In this example, if the arr object is empty, then assign {} to it. This function is similar to

?? Operator, so the | in JS is quite powerful.

Related Article

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.