Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shift "arguments"?

Here's the script:

function runScripts() {     if (arguments.length === 0) return;     chrome.tabs.executeScript(null, {         file: arguments[0]     }, function() {         arguments.shift();         runScripts.apply(null, arguments);     }); } 

It doesn't work because arguments is not actually an array, it's just array-like. So how can I "shift" it or hack off the first element so that I can apply this function recursively?

like image 298
mpen Avatar asked Jan 23 '11 19:01

mpen


People also ask

How do I shift in Bash?

Shift is a builtin command in bash which after getting executed, shifts/move the command line arguments to one position left. The first argument is lost after using shift command. This command takes only one integer as an argument.

What does the shift command do?

The shift command changes the values of the batch parameters %0 through %9 by copying each parameter into the previous one—the value of %1 is copied to %0, the value of %2 is copied to %1, and so on. This is useful for writing a batch file that performs the same operation on any number of parameters.

What is shift 2 in shell script?

So the command shift always discards the previous value of $1, and shift 2 always discards the previous values of $1 and $2.

What does $@ mean?

$@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


2 Answers

var params = Array.prototype.slice.call(arguments); params.shift(); 

You can check out this blog post which explains it in further detail.

like image 60
ClosureCowboy Avatar answered Sep 30 '22 02:09

ClosureCowboy


I assume you want to reference the original arguments, instead of that from the callback you're passing to chrome.tabs.executeScript.

If so, you'll need to cache it first.

function runScripts() {     if (arguments.length === 0) return;     var args = [];     Array.prototype.push.apply( args, arguments );      chrome.tabs.executeScript(null, {         file: args.shift();     }, function() {              // using the modified Array based on the original arguments object         runScripts.apply(null, args);     }); } 
like image 25
user113716 Avatar answered Sep 30 '22 01:09

user113716