Technique for extending the method extend in Jquery

  • 2020-03-30 03:46:45
  • OfStack

Extend is a common parameter handler when developing with Jquery, especially for default values.

The prototype extension method of Jquery is:


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

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

But in use, the default value is often not changed,

As follows:


var defaut={'selector':'select','default':' The default value ','backcolor':'#85e137','forecolor':'#000'};

var src={'selector':'ss','default':' joke ','backcolor':'#fff','forecolor':'red'};

If we use


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

To handle the parameters, so that the default value for the next processing is the value after the processing rather than the actual default value.

We can use the following code:


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

That is, we use an empty object as the target parameter (default), and the default parameter as the first source parameter (SRC), which also returns the merged parameter, but our default is not changed, and can be used again!

So the default value stays the same!


Related articles: