admin管理员组

文章数量:1335847

I found a difference in the handling of

[1,2,3].slice(1, undefined)

between chrome (that returns [2,3]) and firefox (that returns []).

Both of course instead agree on [2, 3] as the value of

[1,2,3].slice(1)

and they also both agree on [] as the value of

[1,2,3].slice(1, null)

Which of the two is correct? Or may be this not clearly specified in the standard?

I found a difference in the handling of

[1,2,3].slice(1, undefined)

between chrome (that returns [2,3]) and firefox (that returns []).

Both of course instead agree on [2, 3] as the value of

[1,2,3].slice(1)

and they also both agree on [] as the value of

[1,2,3].slice(1, null)

Which of the two is correct? Or may be this not clearly specified in the standard?

Share Improve this question asked Aug 6, 2011 at 17:04 65026502 115k16 gold badges174 silver badges274 bronze badges 3
  • What version of Firefox? I don't get that result in FF5 when using undefined. The result from null is expected since it will be converted to 0. – user113716 Commented Aug 6, 2011 at 17:15
  • Are you not going to share what version of FF you were using? It would be interesting to know if this issue still existed in FF4. – user113716 Commented Aug 6, 2011 at 17:39
  • @Patrick: The problem is present in firefox 3.6.18 on ubuntu 10.10. – 6502 Commented Aug 6, 2011 at 22:06
Add a ment  | 

3 Answers 3

Reset to default 8

The specification says:

7. If end is undefined, let relativeEnd be len; else let relativeEnd be ToInteger(end).

Which Firefox version are you using? Firefox 5 gives me correctly [2, 3]. Update: Firefox 3.6 returns indeed an empty array.

I don't know what is wrong here, because if you call slice() without a second parameter, end will be undefined too.

Update:

After playing around a bit, it seems that an empty array is returned if the second parameter passed to .slice() is NaN. Example (+undefined returns NaN):

> [1,2,3].slice(1, +undefined)
> []

This is the same in Firefox and in Chrome.

Unfortunately, this is also not conform to the specification, as ToInteger(NaN) [spec] should return 0, so the the array should actually be sliced to the end.

I don't claim that this is the reason why it does not work properly in some Firefox versions, I don't know the implementation.

Incident of minor disorientation... never mind.

I'm not sure if that is defined on the standard, but I can tell you that is a tricky invocation. Like Felix says, it's not standard behaviour, but I can think of why it's like that.

As you may know undefined is the value set to parameters that are not supplied when a function is called. In your example however, you are actually passing undefined, so the arguments passed are actually two and is reflected in arguments.length.

So from what you say happens it's easy to imagine that in Chrome, the second parameter is being checked with typeof param2 == "undefined"and in Firefox they use arguments.length > 1. So it's not (entirely) that Firefox got it wrong, like Felix suggests, they just assume undefined would never actually be passed in directly as the 2nd parameter.

You'll probably run into several of this cases using undefined like that, I'd remend you dont use it!

The version of Firefox tested in the post is in the wrong here. However, Firefox 5 works per specification and [1,2,3].slice(1, undefined) evaluates to [2,3].

From the ECMAScript ed5 standard:

The slice method takes two arguments, start and end, and returns a substring of the result of converting this object to a String, starting from character position start and running to, but not including, character position end (or through the end of the String if end is undefined)...

Happy coding.

本文标签: