Native js achieves copying objects and extension objects, similar to the extend () method in jquery _ javascript skills

Source: Internet
Author: User
The extend () method of jq can easily implement the extended object method. What we need to implement here is: Native js achieves copying objects and extended objects, similar to the extend () method in jq, you can refer to the extend () method of jq to easily implement the extension object method. The syntax is as follows: $. extend (obj1, boj2, obj3 );

What we need to implement now is: Native js implements copying objects and extension objects, similar to the extend () method in jq. The specific example is as follows:
There are three objects:

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

Goals:

Copy the o1 object and extend the properties and methods of the o2 and o3 objects to the copied objects and output them.

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 () {// extension object var args = arguments; if (args. length <2) return; var temp = cloneObj (args [0]); // call the copy object method for (var n = 1; n <args. length; n ++) {for (var I in 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.