AngularJS extend Usage Details and instance code, angularjsextend
AngularJS extend usage
Angular. extend: copy the second parameter and the first-layer attribute of the subsequent parameter (whether simple or object) to the first-layer attribute of the first parameter, that is, if it is an object, it references the same object and returns the first parameter object.
Instance 1: var r = angular. extend (B, a); copy the first attribute (whether simple or object) of object a to the first attribute of object B, that is, if it is an object, it references the same object and returns object B.
Js Code
var a = { name : 'bijian', address : 'shenzhen', family : { num : 6, amount : '80W' } }; var b = {}; var r = angular.extend(b, a); console.log('a:' + JSON.stringify(a)); console.log('b:' + JSON.stringify(b)); console.log('r:' + JSON.stringify(r)); b.address = 'hanzhou'; b.family.amount = '180W'; console.log('a:' + JSON.stringify(a)); console.log('b:' + JSON.stringify(b)); console.log('r:' + JSON.stringify(r));
Running result:
Text code
a:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}} b:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}} r:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}} a:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"180W"}} b:{"name":"bijian","address":"hanzhou","family":{"num":6,"amount":"180W"}} r:{"name":"bijian","address":"hanzhou","family":{"num":6,"amount":"180W"}}
Example 2: var r = angular. extend (B, a, z); copies the first attribute (simple attribute or object) of objects a and z to the first attribute of object B, that is, if it is an object, it references the same object and returns object B.
Js Code
Var a = {name: 'bijian ', address: 'shenzhen', family: {num: 6, amount: '80w' }}; var z = {family: {amount: '150w', mainSource: 'operating company '}}; var B ={}; var r = angular. extend (B, a, z); console. log ('a: '+ JSON. stringify (a); console. log ('B:' + JSON. stringify (B); console. log ('R: '+ JSON. stringify (r); B. address = 'hanzhou'; B. family. amount = '180w'; console. log ('a: '+ JSON. stringify (a); console. log ('B:' + JSON. stringify (B); console. log ('R: '+ JSON. stringify (r ));
Running result:
Text code
A: {"name": "bijian", "address": "shenzhen", "family": {"num": 6, "amount ": "80 W"} B: {"name": "bijian", "address": "shenzhen", "family": {"amount": "150 W ", "mainSource": "Operating Company" }}r: {"name": "bijian", "address": "shenzhen", "family": {"amount ": "150 W", "mainSource": "Operating Company"} a: {"name": "bijian", "address": "shenzhen", "family ": {"num": 6, "amount": "80 W"} B: {"name": "bijian", "address": "hanzhou", "family ": {"amount": "180 W", "mainSource": "Operating Company" }}r: {"name": "bijian", "address": "hanzhou ", "family": {"amount": "180 W", "mainSource": "Operating Company "}}
More instances are not as simple, direct, and accurate as the source code. The source code of angular. extend is as follows:
Js Code
/** * @ngdoc function * @name angular.extend * @function * * @description * Extends the destination object `dst` by copying all of the properties from the `src` object(s) * to `dst`. You can specify multiple `src` objects. * * @param {Object} dst Destination object. * @param {...Object} src Source object(s). * @returns {Object} Reference to `dst`. */ function extend(dst) { var h = dst.$$hashKey; forEach(arguments, function(obj){ if (obj !== dst) { forEach(obj, function(value, key){ dst[key] = value; }); } }); setHashKey(dst,h); return dst; }
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!