Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-31-2011, 03:39 PM   PM User | #1
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
Why is the callwhy is the slice method only a method of an Array instance?

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:

Code:
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:

Code:
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.
johnmerlino is offline   Reply With Quote
Old 01-31-2011, 03:42 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by johnmerlino View Post
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.
Philip M is offline   Reply With Quote
Old 01-31-2011, 03:46 PM   PM User | #3
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
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.
johnmerlino is offline   Reply With Quote
Old 01-31-2011, 04:35 PM   PM User | #4
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
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...

Last edited by mrhoo; 01-31-2011 at 04:41 PM..
mrhoo is offline   Reply With Quote
Old 01-31-2011, 09:52 PM   PM User | #5
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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:
Code:
Array.prototype.use=function(a){return [].slice.call(a);};

usage:
Code:
alert(    [].use("fred")    )
shows "f,r,e,d"
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%

Last edited by rnd me; 01-31-2011 at 09:55 PM..
rnd me is offline   Reply With Quote
Reply

Bookmarks

Tags
call, instance, object, slice

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:17 PM.


Advertisement
Log in to turn off these ads.