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.