Reputation: 3317
As you know every function is java script has a variable named 'arguments' in itself that contains all arguments passed to the function.
Consider following code sample:
String.prototype.format = function(pattern){
var args = arguments.slice(1);
// other implementations are removed...
}
In this scenario, Google Closure Compiler tells me that arguments has no method slice.
As a matter of fact, it has a method names slice, but Google Closure Compiler can not determinate the type of arguments array.
But at runtime the code works fine
How can I define the type of arguments for Google Closure Compiler?
What is a best practice?
I've tested several methods, but non of them worked for me.
Without this, our project will not compiled correctly, so we need this, thanks
Thanks
Upvotes: 1
Views: 193
Reputation: 122916
arguments
is not an array (it's an array-like object), so it contains no method slice
. You can try: var args = [].slice.call(arguments,1);
In other words, call the Array.slice
-method for the arguments
-object to create a real Array
out of it. To test run this code in a browser console:
function foo(){
console.log([].slice.call(arguments,1));
}
foo(1,2,3); //=> logs [2,3]
Upvotes: 3