Videos
shift removes the first object in an array, and shifts all objects up in the array.
The object which was removed is then returned.
In your example, the object must be a function which itself is then invoked.
This is shorthand for:
var removedFunc = queue.shift();
removedFunc();
See MDN documentation for more info on shift:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
The shift() method removes the first element from an array and returns that element. This method changes the length of the array.
Example (Demo):
var a = [];
a.push(function(){
alert("a");
});
a.push(function(){
alert("b");
});
a.shift()(); //alerts a
a.shift()(); //alerts b
queue is apparently an array of functions. The first function on the queue is being shift()ed off (ie. returned and removed from the array), and then immediately invoked.
If it helps, try:
var nextFunction = queue.shift();
nextFunction();
It's the same thing, just broken down for readability - certainly, I would instinctively see ()() as a typo.
EDIT: You can also make it look "less typo-y" like so:
queue.shift().call();
In this way you are explicitly calling the function - again, it does the exact same thing, but now it looks deliberate.
Logically it does not work and you should reverse your loop:
for (int i = position-1; i >= 0; i--) {
array[i+1] = array[i];
}
Alternatively you can use
System.arraycopy(array, 0, array, 1, position);
Assuming your array is {10,20,30,40,50,60,70,80,90,100}
What your loop does is:
Iteration 1: array[1] = array[0]; {10,10,30,40,50,60,70,80,90,100}
Iteration 2: array[2] = array[1]; {10,10,10,40,50,60,70,80,90,100}
What you should be doing is
Object temp = pool[position];
for (int i = (position - 1); i >= 0; i--) {
array[i+1] = array[i];
}
array[0] = temp;