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

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

WBOY
Release: 2016-05-16 17:43:28
Original
1323 people have browsed it

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template