469,954 Members | 1,812 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,954 developers. It's quick & easy.

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.
Dec 6 '05 #1
4 1927
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.
Dec 6 '05 #2
Michael Winter <m.******@blueyonder.co.uk> 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.
Dec 6 '05 #3
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)

Dec 6 '05 #4
Evertjan. <ex**************@interxnl.net> 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.
Dec 6 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Dave Bazell | last post: by
3 posts views Thread by kr | last post: by
24 posts views Thread by RyanTaylor | last post: by
7 posts views Thread by RubyRedRick | last post: by
18 posts views Thread by dhtml | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.