Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    189
    Thanks
    38
    Thanked 3 Times in 3 Posts

    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.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,159
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    189
    Thanks
    38
    Thanked 3 Times in 3 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,402
    Thanks
    11
    Thanked 595 Times in 575 Posts
    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"
    Last edited by rnd me; 01-31-2011 at 09:55 PM.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •