PDA

View Full Version : Why is the callwhy is the slice method only a method of an Array instance?



johnmerlino
01-31-2011, 04:39 PM
Why is the callwhy is the slice method only a method of an Array instance? The reason why I ask is because if you want to use it for the arguments property of function object, or a string, or an object, or a number instance, you are forced to use Array.prototype.slice.call(). And by doing that, you can pass in any type of object instance (Array, Number, String, Object) into it. So why not just default it as a method of all object instances built into the language?

In other words, instead of doing this:



function Core(){

var obj = {a : 'a', b : 'b'};
var num = 1;
var string = 'aff';

console.log(typeof arguments);//Object
console.log(arguments instanceof Array);//false
var args1 = Array.prototype.slice.call(arguments);
console.log(args1);
var args2 = Array.prototype.slice.call(obj);
console.log(args2);
var args3 = Array.prototype.slice.call(num);
console.log(args3);
var args4 = Array.prototype.slice.call(string);
console.log(args4);

Core('dom','event','ajax');


Why not just be able to do this:



function Core(){

var obj = {a : 'a', b : 'b'};
var num = 1;
var string = 'aff';

var args = arguments.slice(0);
var args2 = obj.slice(0);
var args3 = num.slice(0);
var args4 = string.slice(0);
//right now none of the above would work but it's more convenient than using the call alternative.

}
Core('dom','event','ajax');


Why did the designers of the javascript scripting language make this decision?

Thanks for response.

Philip M
01-31-2011, 04:42 PM
Why did the designers of the javascript scripting language make this decision?



The only reliable way to find out is to ask them. AFAIK it is just one of those things. ;)

johnmerlino
01-31-2011, 04:46 PM
Ok the only thing I could think of is to prevent defining a method in more than one place, therefore having more concise code, but now forcing the end user to write a longer block of code just to access a specific method.

mrhoo
01-31-2011, 05:35 PM
Actually, strings also have a native slice method.
var s='1.A new string...';
alert(s.slice(2,-3))
// returns 'A new string';

But calling Array.prototype.slice on a number object (or any non-array-like object) just returns an empty array...

rnd me
01-31-2011, 10:52 PM
First, note that "[].slice.call" works as well as "Array.prototype.slice.call", and is a LOT less to type: go generics!


the reason that arguments and other indexed objects don't have a slice is performance. Array defines dozens of methods. If arguments used real arrays, we'de have a lot more than the present 20% function call overhead because the interpreter would have to instantiate true arrays with all their methods for every function call. Instantiating the methods of Array would again require more sub-arrays for their arguments, and i don't know how far down the rabbit hole it would continue. safe to say, it would be much slower.


the reason DOMCollection objects don't have slice is because they aren't arrays: they are collections. Arrays are static, collections are live: removing an element from the dom also removes it from the collection. This special behavior cannot easily be mimicked using an array, so a distinction is necessary.

shortcut:

Array.prototype.use=function(a){return [].slice.call(a);};


usage:


alert( [].use("fred") )
shows "f,r,e,d"