在JavaScript中,“1..something”是什么意思? [英] What does `1..something` mean in JavaScript?

查看:148
本文介绍了在JavaScript中,“1..something”是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script>
1..z
</script>

这不会产生语法或运行时错误。看起来数字和变量名称可以是任何其他名称( 123..qwerty )。我想知道这句话是什么意思?

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?

推荐答案

一个范围, 1..z 表达式只返回 undefined

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

为什么?

第一个点结束了 Numeric Literal ,为您提供 Number 原语:

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

var n = 1.;

Numeric Literal的语法表示如下:

The grammar of a Numeric Literal is expressed like this:

DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt 

正如您可以看到点后的DecimalDigits部分可选 (opt后缀)。

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

第二个点是属性访问者,它只会尝试获取 z 属性, undefined ,因为它不存在于 Number.prototype 对象中:

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'](); 

这篇关于在JavaScript中,“1..something”是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆