AngularJS extend Usage Details and instance code, angularjsextend

Source: Internet
Author: User

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!

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.