15.4.4.9 Array.prototype.shift()

2010-07-05

mixed Array.prototype.shift()

Remove the first element from the array and return it.

If the array is empty, length is forced to 0.

The this value is coerced to an object.

Code: (Meta Ecma)
Array.prototype.shift = function(){
var O = ToObject(this);
var lenVal = O.[[Get]]("length");
var len = ToUint32(lenVal);
if (len == 0) {
O.[[Put]]("length", 0);
return undefined;
}
var first = O.[[Get]]("0");
var k = 1;
while (k < len) {
var from = ToString(k);
var to = ToString(k-1);
var fromPresent O.[[HasProperty]](from);
if (fromPresent) {
var fromVal = O.[[Get]](from);
O.[[Put]](to, fromVal, true);
} else {
O.[[Delete]](to, true);
}
++k;
}
O.[[Delete]](ToString(len-1), true);
O.[[Put]]("length", len-1, true);
return first;
}