Home > php教程 > PHP开发 > body text

AngularJS extend usage details and example code

高洛峰
Release: 2016-12-06 16:24:31
Original
1496 people have browsed it

AngularJS extend usage

Angular.extend: Copy and assign the first-level properties of the second parameter and subsequent parameters (whether they are simple properties or objects) to the first-level properties of the first parameter, that is, if it is an object , it refers to the same object and returns the first parameter object.

                                                            var r = angular.extend(b, a); Copy and assign the first-level properties of object a (whether it is a simple property or an object) to the first-level properties of object b, that is, if it is an object, It refers to the same object and returns the 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));
Copy after login

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"}}
Copy after login

Example 2: var r = angular.extend( b, a, z); successively copy and assign the first-level attributes of objects a and z (whether they are simple attributes or objects) to the first-level attributes of object b, that is, if it is an object, it refers to the same object. , and return the object b

Js code

var a = {
  name : 'bijian',
  address : 'shenzhen',
  family : {
    num : 6,
    amount : '80W'
  }
};
var z = {
  family : {
    amount : '150W',
    mainSource : '经营公司'
  }
};
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));
Copy after login

Run result:

Text code

a:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}}
b:{"name":"bijian","address":"shenzhen","family":{"amount":"150W","mainSource":"经营公司"}}
r:{"name":"bijian","address":"shenzhen","family":{"amount":"150W","mainSource":"经营公司"}}
a:{"name":"bijian","address":"shenzhen","family":{"num":6,"amount":"80W"}}
b:{"name":"bijian","address":"hanzhou","family":{"amount":"180W","mainSource":"经营公司"}}
r:{"name":"bijian","address":"hanzhou","family":{"amount":"180W","mainSource":"经营公司"}}
Copy after login

No matter how many examples there are, it is not as simple, direct and simple as the source code Accurate, angular. The extend source code 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;
}
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template