In addition to normal usage, slice is often used to convert array-like objects to true array.
Noun explanation: array-like object – Objects with length attributes, such as { 0: ‘foo', length: 1 }, or even { length: ‘bar' }. The most common array-like objects are arguments and NodeList.
By looking at the source code of the V8 engine, you can simplify the internal implementation of slice to:
function slice(start, end) {
var len = ToUint32(), result = [];
for(var i = start; i < end; i++) {
(this[i]);
}
return result;
}
It can be seen that slice does not need this to be of array type, it only needs to have the length attribute. And the length attribute can not be of type number. When it cannot be converted to a numeric value, ToUnit32() returns 0.
For standard browsers, the principle of slice has been explained clearly above. But the annoying ie always makes us mess up:
var slice = ;
(); // => IE: Object expected.
(); // => IE: JScript object expected.
The above code reports an error in ie. The hateful IE's Trident engine is not open source, so we can only guess:
function ie_slice(start, end) {
var len = ToUint32(), result = [];
if(__typeof__ this !== 'JScript Object') throw 'JScript object expected';
if(this === null) throw 'Oject expected';
for(var i = start; i < end; i++) {
(this[i]);
}
return result;
}
At this point, the slutty ie was finished.
There is another topic about slice: Use [].slice? Theoretically, [] needs to create an array, which will be slightly worse than . But in fact, these two are similar, just like using i++ or ++i in a loop, it is purely a personal habit.
The last topic, about performance. For array filtering, there is a way to write sacrificial hues:
var ret = [];
for(var i = start, j = 0; i < end; i++) {
ret[j++] = arr[i];
}
Exchange space for time. Remove push, for large arrays, the performance improvement is still quite obvious.
I wrote a blog early in the morning and was not in a good mood. I have to leave a question for everyone:
var slice = ;
alert(({0: 'foo', length: 'bar'})[0]); // ?
alert((NaN).length); // ?
alert(({0: 'foo', length: '100'})[0]); // ?
Noun explanation: array-like object – Objects with length attributes, such as { 0: ‘foo', length: 1 }, or even { length: ‘bar' }. The most common array-like objects are arguments and NodeList.
By looking at the source code of the V8 engine, you can simplify the internal implementation of slice to:
Copy the codeThe code is as follows:
function slice(start, end) {
var len = ToUint32(), result = [];
for(var i = start; i < end; i++) {
(this[i]);
}
return result;
}
It can be seen that slice does not need this to be of array type, it only needs to have the length attribute. And the length attribute can not be of type number. When it cannot be converted to a numeric value, ToUnit32() returns 0.
For standard browsers, the principle of slice has been explained clearly above. But the annoying ie always makes us mess up:
Copy the codeThe code is as follows:
var slice = ;
(); // => IE: Object expected.
(); // => IE: JScript object expected.
The above code reports an error in ie. The hateful IE's Trident engine is not open source, so we can only guess:
Copy the codeThe code is as follows:
function ie_slice(start, end) {
var len = ToUint32(), result = [];
if(__typeof__ this !== 'JScript Object') throw 'JScript object expected';
if(this === null) throw 'Oject expected';
for(var i = start; i < end; i++) {
(this[i]);
}
return result;
}
At this point, the slutty ie was finished.
There is another topic about slice: Use [].slice? Theoretically, [] needs to create an array, which will be slightly worse than . But in fact, these two are similar, just like using i++ or ++i in a loop, it is purely a personal habit.
The last topic, about performance. For array filtering, there is a way to write sacrificial hues:
Copy the codeThe code is as follows:
var ret = [];
for(var i = start, j = 0; i < end; i++) {
ret[j++] = arr[i];
}
Exchange space for time. Remove push, for large arrays, the performance improvement is still quite obvious.
I wrote a blog early in the morning and was not in a good mood. I have to leave a question for everyone:
Copy the codeThe code is as follows:
var slice = ;
alert(({0: 'foo', length: 'bar'})[0]); // ?
alert((NaN).length); // ?
alert(({0: 'foo', length: '100'})[0]); // ?