Reputation: 41
temp = {0:'one', 1:'two', 2:'three', 3:'four',length:4};
console.log( Array.prototype.slice.call( temp, 1));
//["two", "three", "four"]
Why is this the result? Where is the length
property? Shouldn't it be ["two", "three", "four", 4]
when Array.prototype.slice.call( temp, 1)
is called?
Upvotes: 1
Views: 47
Reputation: 3541
Simplified version of slice:
Array.prototype.slice = function(a, b) {
a = a || 0
if (a < 0) a += this.length
b = b || this.length
if (b < 0) b += this.length
var ret = []
for (var i = a; i < b; i++)
ret.push(this[i])
return ret
}
So actually slice function uses []
operator and .length
property on this
. That's how it works on arrays and array-like objects (those which have []
and .length
)
Upvotes: 3