AngularJS extend usage
: Copy the first layer attributes of the second parameter and subsequent parameters (whether it is a simple property or an object) to the first layer attribute of the first parameter, that is, if it is an object, it refers to the same object and returns the first parameter object.
Example 1: var r = (b, a); Copy the first layer attribute of object a (whether it is a simple attribute or an object) to the first layer attribute of object b, that is, if it is an object, it refers to the same object and returns object b
Js code
var a = { name : 'bijian', address : 'shenzhen', family : { num : 6, amount : '80W' } }; var b = {}; var r = (b, a); ('a:' + (a)); ('b:' + (b)); ('r:' + (r)); = 'hanzhou'; = '180W'; ('a:' + (a)); ('b:' + (b)); ('r:' + (r));
Running results:
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 = (b, a, z); successively copy the first layer attributes of objects a and z (whether it is a simple attribute or an object) to the first layer attribute of object b. That is, if it is an object, it refers to 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 : 'Operation of a company' } }; var b = {}; var r = (b, a, z); ('a:' + (a)); ('b:' + (b)); ('r:' + (r)); = 'hanzhou'; = '180W'; ('a:' + (a)); ('b:' + (b)); ('r:' + (r));
Running results:
Text code
a:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}} b:{"name":"bijian","address":"shenzhen","family":{"amount":"150W","mainSource":"Operation of a company"}} r:{"name":"bijian","address":"shenzhen","family":{"amount":"150W","mainSource":"Operation of a company"}} a:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}} b:{"name":"bijian","address":"hanzhou","family":{"amount":"180W","mainSource":"Operation of a company"}} r:{"name":"bijian","address":"hanzhou","family":{"amount":"180W","mainSource":"Operation of a company"}}
No matter how many instances there are, it is not as simple, direct and accurate as the source code. The source code is as follows:
Js code
/** * @ngdoc function * @name * @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, I hope it can help you. Thank you for your support for this site!