array.slice() question

Discussion in 'Javascript' started by Christopher Benson-Manica, Dec 6, 2005.

  1. Is array.slice() guaranteed to return a zero-length array if the first
    argument is greater than the length of the array? Or is it allowed to
    throw an exception?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Dec 6, 2005
    #1
    1. Advertising

  2. On 06/12/2005 16:58, Christopher Benson-Manica wrote:

    > Is array.slice() guaranteed to return a zero-length array if the
    > first argument is greater than the length of the array?


    In a conforming implementation of ECMA-262, yes. I can't say that I've
    ever called it with out-of-bounds arguments, though.

    > Or is it allowed to throw an exception?


    Built-in methods only throw exceptions in very specific and
    unrecoverable circumstances. For instance, a syntax error in an eval
    call, or a RegExp or Function constructor call, or applying non-generic
    prototyped methods (like valueOf) to instances of different objects.
    Arguments are usually normalized to values that make sense, especially
    those that can be bounded to range.

    Mike

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
     
    Michael Winter, Dec 6, 2005
    #2
    1. Advertising

  3. Michael Winter <> wrote:

    > In a conforming implementation of ECMA-262, yes. I can't say that I've
    > ever called it with out-of-bounds arguments, though.


    I'm creating a copy of an array with one item removed, like so:

    var copyWithItemRemoved=someArray.slice( 0, idx ).concat( someArray.slice(idx+1) );

    Am I missing a better way to do this? (See my next post, I will ask
    in a new topic.)

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Dec 6, 2005
    #3
  4. Christopher Benson-Manica

    Evertjan. Guest

    Christopher Benson-Manica wrote on 06 dec 2005 in comp.lang.javascript:

    > I'm creating a copy of an array with one item removed, like so:
    >
    > var copyWithItemRemoved=someArray.slice( 0, idx ).concat(
    > someArray.slice(idx+1) );
    >
    > Am I missing a better way to do this? (See my next post, I will ask
    > in a new topic.)
    >


    var theRemevedItem = someArray.splice(idx, 1)
    var copyWithItemRemoved = someArray

    --
    Evertjan.
    The Netherlands.
    (Replace all crosses with dots in my emailaddress)
     
    Evertjan., Dec 6, 2005
    #4
  5. Evertjan. <> wrote:

    > var theRemevedItem = someArray.splice(idx, 1)
    > var copyWithItemRemoved = someArray


    That isn't what I want, though - splice also modifies the original
    array, which I am explicitly trying to avoid. I could, of course, do

    var copyWithItemRemoved=someArray.slice( 0 );
    copyWithItemRemoved.splice( idx, 1 );

    but I'm not enthusiastic.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Dec 6, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Bazell

    slice of multidimensional array

    Dave Bazell, Jul 23, 2003, in forum: Perl
    Replies:
    2
    Views:
    4,137
  2. kr

    array slice question

    kr, Aug 10, 2004, in forum: Perl
    Replies:
    3
    Views:
    687
  3. Replies:
    9
    Views:
    275
    David Squire
    May 22, 2006
  4. Rick
    Replies:
    9
    Views:
    138
    Ted Zlatanov
    Jul 31, 2007
  5. RubyRedRick

    Newbyish question about Array slice method

    RubyRedRick, Jun 1, 2008, in forum: Javascript
    Replies:
    4
    Views:
    135
    Thomas 'PointedEars' Lahn
    Jun 1, 2008
Loading...

Share This Page