2

I want to create a copy of my arguments inside a logging function using angular.copy().

Since the arguments is already an Array, I expected to get an Array but it returned Object instead of Array.

$scope.log = function(argN) {
    console.log("arguments", arguments, angular.copy(arguments));
    if (typeof(console) !== 'undefined') {
        console.log.apply(console, angular.copy(arguments));
    }
}
  1. Is this some sort of standard practice of copying?
  2. How can I get Array instead of Object?
1
  • 1
    arguments is not an array. Commented Sep 6, 2016 at 17:32

2 Answers 2

2

arguments is not an array but array like. To do a shallow clone of arguments, use Array.prototype.slice.call(arguments). That will create an array of all the arguments. From there, to get a deep clone, use angular.copy.

var foo = angular.module('foo', []);

foo.controller('bar', function($scope) {
   $scope.trace = function() {
       var clonedArguments = angular.copy(arguments);
       console.log(clonedArguments);
       var clonedArgs = angular.copy(Array.prototype.slice.call(arguments));
       console.log(clonedArgs);
   };
  $scope.trace(1, 'foo', { bar: 27 })
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="foo" ng-controller="bar"></div>

Sign up to request clarification or add additional context in comments.

Comments

0

Edit: I think console.dir() maybe what you are looking for after reading this

Print JSON parsed object?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.