Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
72
rated 0 times [  74] [ 2]  / answers: 1 / hits: 39014  / 11 Years ago, tue, may 28, 2013, 12:00:00

What is the difference between these two extend functions?



  angular.extend(a,b);
$.extend(a,b);


While the jquery.extend is well documented the angular.extend lacks details and the comments there provide no answers. (https://docs.angularjs.org/api/ng/function/angular.extend).



Does angular.extend also provide deep copy?


More From » jquery

 Answers
15

angular.extend and jQuery.extend are very similar. They both do a shallow property copy from one or more source objects to a destination object. So for instance:



var src = {foo: bar, baz: {}};
var dst = {};
whatever.extend(dst, src);
console.log(dst.foo); // bar
console.log(dst.baz === src.baz); // true, it's a shallow copy, both
// point to same object


angular.copy provides a deep copy:



var src = {foo: bar, baz: {}};
var dst = angular.copy(src);
console.log(dst.baz === src.baz); // false, it's a deep copy, they point
// to different objects.


Getting back to extend: I only see one significant difference, which is that jQuery's extend allows you to specify just one object, in which case jQuery itself is the target.



Things in common:




  • It's a shallow copy. So if src has a property p that refers to an object, dst will get a property p that refers to the same object (not a copy of the object).


  • They both return the destination object.


  • They both support multiple source objects.


  • They both do the multiple source objects in order, and so the last source object will win in case more than one source object has the same property name.




Test page: Live Copy | Live Source



<!DOCTYPE html>
<html>
<head>
<script src=http://code.jquery.com/jquery-1.9.1.min.js></script>
<script src=http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js></script>
<meta charset=utf-8 />
<title>Extend!</title>
</head>
<body>
<script>
(function() {
use strict;
var src1, src2, dst, rv;

src1 = {
a: I'm a in src1,
b: {name: I'm the name property in b},
c: I'm c in src1
};
src2 = {
c: I'm c in src2
};

// Shallow copy test
dst = {};
angular.extend(dst, src1);
display(angular shallow copy? + (dst.b === src1.b));
dst = {};
jQuery.extend(dst, src1);
display(jQuery shallow copy? + (dst.b === src1.b));
$(<hr>).appendTo(document.body);

// Return value test
dst = {};
rv = angular.extend(dst, src1);
display(angular returns dst? + (rv === dst));
dst = {};
rv = jQuery.extend(dst, src1);
display(jQuery returns dst? + (rv === dst));
$(<hr>).appendTo(document.body);

// Multiple source test
dst = {};
rv = angular.extend(dst, src1, src2);
display(angular does multiple in order? +
(dst.c === src2.c));
dst = {};
rv = jQuery.extend(dst, src1, src2);
display(jQuery does multiple in order? +
(dst.c === src2.c));

function display(msg) {
$(<p>).html(String(msg)).appendTo(document.body);
}
})();
</script>
</body>
</html>

[#77964] Monday, May 27, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryderalfonsos

Total Points: 655
Total Questions: 88
Total Answers: 91

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
ryderalfonsos questions
Mon, Sep 9, 19, 00:00, 5 Years ago
Wed, Feb 13, 19, 00:00, 5 Years ago
Tue, Feb 12, 19, 00:00, 5 Years ago
Fri, Dec 28, 18, 00:00, 6 Years ago
;