JS copies all object s attributes to object r (native js + jquery) _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to copy the attributes of all object s to object r in js. Native js + jquery implements native writing:

The Code is as follows:


/**
* Copy all s attributes to r
* @ Param r {Object}
* @ Param s {Object}
* @ Param is_overwrite {Boolean} if it is set to false, the existing value is not overwritten. Other values
* Undefined indicates that the attributes with the same name in s will overwrite the values 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;
}

JQuery is easy to write.

The Code is as follows:


Var a = {
Aa: 1,
AB: 2
};
Var B = {
Ba: 1,
Bb: 2
};

$. Extend (a, B );
Console.info ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.