Why use Array.prototype.slice.call with arguments

I call the using apply method, and I don't know how many arguments I will pass:

At the moment, my code looks like this:

selectFolder: function(e){
  e.preventDefault();

  this.addSelectedClass.apply(this, Array.prototype.slice.call(arguments));
},

The only reason I use Array.prototype.slice is because it is contained in most examples.

Why don't I just pass the following arguments:

 this.addSelectedClass.apply(this, arguments);
+5
source share
2 answers

argumentss apply()are accurate

When called applyfor a function, it is enough to use the original one arguments. SO in your example, you can easily replace the line with the following:

this.addSelectedClass.apply(this, arguments);

argumentsc call()are not accurate

call, arguments ( slice()), /. call() , , , .

arguments . (, length), .

Mozilla Deveveloper

,

, apply() call(), :

func.apply(thisArg[, argsArray]);
func.call(thisArg[, arg1[, arg2[, ...]]]);

.

  • apply() . slice() , arguments .

  • call() , . , , , .

, .

+5

arguments . Array.prototype.slice map forEach. Array.prototype. arguments length.

, , arguments.

+3

All Articles