Home  >  Article  >  Web Front-end  >  JS copies the attributes of all object s to object r (native js jquery)_javascript skills

JS copies the attributes of all object s to object r (native js jquery)_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:02:381094browse

Original writing:

Copy code The code is as follows:

/**
* Copy all the attributes of s to r
* @param r {Object}
* @param s {Object}
* @param is_overwrite {Boolean} If false is specified, it will not be overwritten. Existing values, other values ​​
* including undefined, all mean that the property with the same name in s will overwrite the value in r
*/
mix : function (r, s, is_overwrite) { //TODO:
if (!s || !r) return r;

for (var p in s) {
if (is_overwrite !== false || !(p in r)) {
r[p] = s[p];
}
}
return r;
}

It’s so convenient to write with jQuery

Copy code The code is as follows:

var a={
aa:1,
ab:2
};
var b={
ba:1,
bb:2
};

$.extend(a,b);
console.info(a);

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