JS copies all object s attributes to object r

Source: Internet
Author: User

This article mainly introduces how to copy all the attributes of object s to object r in js. Native js + jquery implements

Native Syntax: the code is as follows: /*** copy all s attributes to r * @ param r {Object} * @ param s {Object} * @ param is_overwrite {Boolean}. If this parameter is specified as false, the existing value is not overwritten. Other values * Including undefined indicate that the attributes with the same name in s will overwrite the values */mix: function (r, s, is_overwrite) in r) {// TODO: if (! S |! R) return r; for (var p in s) {if (is_overwrite! = False |! (P in r) {r [p] = s [p] ;}} return r;} If jQuery is written, it is too convenient to copy the Code as follows: var a = {aa: 1, AB: 2}; var B = {ba: 1, bb: 2}; $. extend (a, B); console.info ();

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.