Home  >  Article  >  Web Front-end  >  Js-$.extend extension method makes method parameters more flexible_javascript skills

Js-$.extend extension method makes method parameters more flexible_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:43:281241browse

When doing JS development, we encapsulate complex third-party plug-ins and then expose a very simple method interface to the outside world. This is a commonly used method during development. In JS, our method parameters usually use JQ’s $.extend extension. Method to implement

Copy code The 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);
}

When the page is called:
Copy code The code is as follows:


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 a certain attribute of the arr object?

Copy code The code is as follows:

In the above code, we changed the nature of the name attribute, and the result output is :


That is, it only modifies the value of the specified attribute, and other values ​​of the object remain at their original default values.
The || operator in JS has the same function as the || , this function is similar to

in C#? ? operator, so || in JS is quite powerful, haha.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn