<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script src="js/angular.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> //angular.copy is deep copy var o1 = { name: ‘xudongyang‘, age: 26, skill:{} } var o2 = angular.copy(o1); console.log(o2); console.log(o1 == o2); //extend is shallow copy of the properties of the source objects from right to left var src1 = { name: ‘xudongyang‘, age: 20 } var src2 = { age: 10, skill: {} } var dst = {}, dst2 = angular.extend(dst , src2 , src1) console.log(dst2.skill == dst.skill); src2.skill.eat = "kaorou"; console.log(dst2.skill); console.log(dst.skill); //angular.merge is deep copy var m1 = {name:‘xudongyang‘ , age:29}, m2 = {age:10 , skill:{}} dstm = {}; dstm2 = angular.merge(dstm , m2 , m1); console.log(dstm2); console.log(m2.skill == dstm2.skill); //angular.extend vs angular.merge var s1 = {skill:{name:‘java‘ , experience: 20, certified: true}}, s2 = {skill:{name:‘js‘ , experience: 10}}, dst = {}, dst2 = angular.extend(dst , s1 , s2); console.log(dst); //angular.extend vs angular.merge var s1 = {skill:{name:‘java‘ , experience: 20, certified: true}}, s2 = {skill:{name:‘js‘ , experience: 10}}, dst = {}, dst2 = angular.merge(dst , s1 , s2); console.log(dst); //浅克隆只克隆一层,深克隆一直复制到最底层 </script> </body> </html>
时间: 2024-12-24 16:02:48