<script>
1..z
<开发者_Go百科/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?
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
精彩评论