Connecting Tech Pros Worldwide Help | Site Map

array.slice() question

 
LinkBack Thread Tools Search this Thread
  #1  
Old December 6th, 2005, 04:07 PM
Christopher Benson-Manica
Guest
 
Posts: n/a
Default array.slice() question

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.

  #2  
Old December 6th, 2005, 06:25 PM
Michael Winter
Guest
 
Posts: n/a
Default Re: array.slice() question

On 06/12/2005 16:58, Christopher Benson-Manica wrote:
[color=blue]
> Is array.slice() guaranteed to return a zero-length array if the
> first argument is greater than the length of the array?[/color]

In a conforming implementation of ECMA-262, yes. I can't say that I've
ever called it with out-of-bounds arguments, though.
[color=blue]
> Or is it allowed to throw an exception?[/color]

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.
  #3  
Old December 6th, 2005, 07:35 PM
Christopher Benson-Manica
Guest
 
Posts: n/a
Default Re: array.slice() question

Michael Winter <m.winter@blueyonder.co.uk> wrote:
[color=blue]
> In a conforming implementation of ECMA-262, yes. I can't say that I've
> ever called it with out-of-bounds arguments, though.[/color]

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.
  #4  
Old December 6th, 2005, 07:45 PM
Evertjan.
Guest
 
Posts: n/a
Default Re: array.slice() question

Christopher Benson-Manica wrote on 06 dec 2005 in comp.lang.javascript:
[color=blue]
> 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.)
>[/color]

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

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

  #5  
Old December 6th, 2005, 08:05 PM
Christopher Benson-Manica
Guest
 
Posts: n/a
Default Re: array.slice() question

Evertjan. <exjxw.hannivoort@interxnl.net> wrote:
[color=blue]
> var theRemevedItem = someArray.splice(idx, 1)
> var copyWithItemRemoved = someArray[/color]

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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.