Change the first argument in a function

I would like to do something like this:

function start(){ // Change the first argument in the argument list arguments[0] = '<h1>' + arguments[0] + '</h1>'; // Call log with the new arguments // But outputs: TypeError: Illegal invocation log.apply(this, arguments); } function log(){ console.log(arguments); // should output -> ['<h1>hello<h1>', 'world', '!'] } start('hello', 'world', '!'); 
+4
source share
1 answer

Your code really works (I just tested it in Firefox, latest version).

However, I could suggest that some implementations might have a problem with the arguments object when passing as the value of Function.prototype.apply . So try:

 function start(){ var args = Array.prototype.slice.call( arguments ); args[0] = '<h1>' + args[0] + '</h1>'; log.apply(this, args); } 

By calling Array.prototype.slice on an arguments object, we create a "true" ECMAscript array that we may need as a second argument to .apply()

+5
source

All Articles