Native JS implements replication objects, extended objects similar to the Extend () method in jquery _javascript techniques

Source: Internet
Author: User
Tags extend

JQ's Extend () method can conveniently implement the extended object method, the syntax is as follows: $.extend (OBJ1,BOJ2,OBJ3);

Now to achieve is: the original JS implementation of replication objects, extended objects, similar to the JQ in the Extend () method, specific examples are as follows:
Existing 3 objects literal:

var o1={hello:1,old:555},
O2 = {
abc:55555555,
hello:2,
fun:function () {
alert ()}
} ,
o3={third:9999};

To achieve the goal:

Copy the O1 object, extend the O2,o3 object properties and methods into the previously copied object and output.

 <script> var o1={hello:1,old:555}, O2 = {abc:55555555, hello:2, Fun:function () {
Alert (111);
}, o3={third:9999};  function Cloneobj (oldobj) {//Copy object method if (typeof (Oldobj)!= ' object ') return oldobj; if (oldobj = null) return oldobj; var
NEWOBJ = new Object ();
for (var i in oldobj) newobj[i] = Cloneobj (Oldobj[i]);
return NEWOBJ;
}; function Extendobj () {//Extended object var args = arguments; if (Args.length < 2) return; var temp = Cloneobj (args[0));//Call Replication Object
Method for (var n = 1; n < args.length n++) {for (var i-args[n]) {temp[i] = args[n][i];} return temp;
} var t=extendobj (O1,O2,O3);
Console.log (t);
Console.log (O1);
Console.log (O2);
Console.log (O3); </script> 

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.