Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > array.slice() question

Reply
Thread Tools

array.slice() question

 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-06-2005
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.
 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      12-06-2005
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.
 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-06-2005
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.
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      12-06-2005
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)

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-06-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments