Home > Web Front-end > JS Tutorial > body text

Tips for using the extension method extend in Jquery_jquery

WBOY
Release: 2016-05-16 16:38:40
Original
1340 people have browsed it

In the process of using Jquery to develop, extend is a commonly used parameter processing function, especially the use of default values.

Jquery’s extension method prototype is:

var v=$.extend(dest,src1,src2,[,src3...]);
Copy after login

The function is to merge src1, src2, src3 into dest and return the merged dest.

But during use, the default value often cannot be changed,

is as follows:

var defaut={'selector':'select','default':'默认值','backcolor':'#85e137','forecolor':'#000'};

var src={'selector':'ss','default':'笑话','backcolor':'#fff','forecolor':'red'};
Copy after login

If we use

var v=$.extend(dfault,src);
Copy after login

to process the parameters, then the default value the next time it is processed will be the value after this processing instead of the real default value.

We can use the following code:

var v=$.extend({},dfault,src);
Copy after login

That is, we use an empty object as the target parameter (default) and the default parameter as the first source parameter (src). In this way, the merged parameters are also returned, but our default has not been changed, which is OK Use it again!

This achieves the effect of keeping the default value unchanged!

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