最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What does `1..something` mean in JavaScript? - Stack Overflow

programmeradmin0浏览0评论
<script>
1..z
</script>

This gives no syntax or runtime error. Looks like number and variable name can be any other (123..qwerty). I'm wondering what does this statement mean?

<script>
1..z
</script>

This gives no syntax or runtime error. Looks like number and variable name can be any other (123..qwerty). I'm wondering what does this statement mean?

Share Improve this question asked Feb 19, 2010 at 22:58 RomanRoman 4,61110 gold badges43 silver badges71 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 33

Is not a range, the 1..z expression will simply return undefined.

Why?

The first dot ends a representation of a Numeric Literal, giving you a Number primitive:

var n = 1.;

The grammar of a Numeric Literal is expressed like this:

DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt 

As you can see the DecimalDigits part after the dot is optional (opt suffix).

The second dot is the property accessor, it will try only to get the z property, which is undefined because it doesn't exist on the Number.prototype object:

1..z; // undefined
1..toString(); // "1"

Is equivalent to access a property with the bracket notation property accessor:

1['z']; // or
1['toString'](); 

Combine these:

alert(1.foo); // --> parse error
alert(1.4.foo); // --> undefined - number 1.4 doesn't have the property foo
alert(1.); // --> 1 (?)

To the conclusion:

alert(1..foo); // --> undefined
发布评论

评论列表(0)

  1. 暂无评论