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