Angularjs extend usage and example code _ANGULARJS

Source: Internet
Author: User
Tags extend

Angularjs Extend Usage

Angular.extend: Assigns the first-level property of the second parameter and subsequent arguments, whether simple or object, to the first-level property of the first argument, that is, if it is an object, refers to the same object and returns the first parameter object.

Instance one: var r = angular.extend (b, a); assigns the first-level property of object A (whether a simple property or an object) to the first-level property 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 = 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)); 

Run 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 two: var r = angular.extend (b, A, z), successively assigns the first-level attribute of object A, z, whether a simple property or an object, to the first layer 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: ' 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)); 

Run Result:

Text Code

a:{' name ': ' Bijian ', ' address ': ' Shenzhen ', ' family ': {"num": 6, "Amount": "80W"} 
b:{"name": "Bijian", "Address": " Shenzhen "," Family ": {" Amount ":" 150W "," Mainsource ":" Operating Company "} 
r:{" name ":" Bijian "," Address ":" Shenzhen "," Family " : {"Amount": "150W", "Mainsource": "Operating Company"} 
a:{"name": "Bijian", "Address": "Shenzhen", "Family": {"num": 6, "Amount" : "80W"} 
b:{"name": "Bijian", "Address": "Hanzhou", "family": {"Amount": "180W", "Mainsource": "Operating Company"} 
r:{" Name ":" Bijian "," Address ":" Hanzhou "," family ": {" Amount ":" 180W "," Mainsource ":" Operating Company "}} 
 

More examples than the source code to the simple, direct and accurate, angular.extend source 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, I hope to help you, thank you for your support for this site!

Related Article

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.