banana banana - 7 months ago 30
Javascript Question

angular.extend has 2 args or 3 args

In the angularjs documentation, it said that

angular.extend
extends the destination object dst by copying own enumerable properties from the
src
object(s) to
dst
. If you want to preserve original objects, you can do so by passing an empty object as the target:
var object = angular.extend({}, object1, object2)
.

I am quite confused with the number of
args
in the statement above. Because the usage of
angular.extend
should be 2
args
:

angular.extend(dst, src);


Thus,
angular.extend
has 2
args
or 3
args
on earth?

Answer

It 'has both' 2 and 3 args.

Taken from the source:

 * @param {Object} dst Destination object.
 * @param {...Object} src Source object(s).
 * @returns {Object} Reference to `dst`.
 */
function extend(dst) {
  return baseExtend(dst, slice.call(arguments, 1), false);
}

Angular takes the arguments from index 1 and extends them onto the destination of parameter dst.

The JSDoc explains that you can have multiple source objects that will go into the destination. You could even have 4, 5, or more arguments provided.