[英]How many literals are there in javascript?
I was working on some project involving regex and suddenly encountered a regex literal
which looks like this: 我正在从事一个涉及正则表达式的项目,突然遇到了一个
regex literal
,如下所示:
/ab+c/g
I know that in programming languages there are some fixed list of possible literals, like in C language integer
, float
etc. 我知道在编程语言中有一些固定的可能文字,如C语言中的
integer
, float
等。
Then I searched for the list of literals supported in javascript but could not found satisfactory answer. 然后,我搜索了javascript支持的文字列表,但找不到满意的答案。
I experimented with node prompt and got following interesting results: 我对节点提示符进行了试验,得到了以下有趣的结果:
> typeof /ab+c/g
'object'
> str = 'xyz'
'xyz'
> typeof `abc ${str}`
'string'
> typeof function f(x, y) {
... return x + y;
... }
'function'
> typeof {
... 'a': 'b'
... }
'object'
This proves that 这证明
regex literal
is essentially object literal
regex literal
本质上是object literal
template literal
is essentially string literal
template literal
本质上是string literal
function literal
is function literal
function literal
是function literal
javascript object literal
is object literal
javascript object literal
是object literal
Even though last one is okay and defined in many places but it doesn't make sense to me that regex literal is still object literal
. 尽管最后一个可以并且在许多地方都定义了,但是对我而言,正则表达式文字仍然是
object literal
并不有意义。
Where is it written? 它写在哪里? How can I find out list of possible literals in javascript?
如何在javascript中找出可能的文字列表?
Take a look at appendix A of the spec and you'll find definitions of StringLiteral , etc. Btw, the spec uses FunctionExpression , not FunctionLiteral . 看一下规范的附录A,您会找到StringLiteral等的定义。顺便说一句,该规范使用FunctionExpression而不是FunctionLiteral 。
Also relevant is 11.8 Literals . 同样重要的是11.8文字 。 Thereunder are
下面是
null
null
true
| true
| false
Notably, undefined
is not a literal. 值得注意的是,
undefined
不是文字。
As that section makes clear, "literal" refers to abbreviated syntax, and does not relate to any object/primitive distinction. 正如该部分所清楚指出的那样,“文字”是指缩写语法,与任何对象/原始区分无关。
Elsewhere in the text (chapter 12 PrimaryExpression ) you'll see terms like ObjectLiteral and ArrayLiteral but those are also referred to as {Object,Array}Initializer s. 在文本的其他地方(第12章PrimaryExpression ),您会看到诸如ObjectLiteral和ArrayLiteral之类的术语,但它们也被称为{Object,Array} Initializer 。
You might avoid thinking too hard about typeof
results. 您可能会避免过分思考
typeof
结果。 While occasionally useful for determining what kind of value a variable holds, it's not really same as the object type in the sense you know it in C or OOP languages. 虽然有时可以用来确定变量具有哪种类型的值,但就您在C或OOP语言中所知的意义而言,它实际上与对象类型并不相同。
Observe: 观察:
typeof (()=>{})
> "function"
(()=>{}) instanceof Object
> true
Also: 也:
typeof ""
> "string"
typeof new String("")
> "object"
"" instanceof String
> false
To answer your main question, there are the following literals: 要回答您的主要问题,有以下文字:
()=>{}
lambda literal ()=>{}
lambda文字
typeof ()=>{} == "function"
function() {}
function literal function() {}
函数文字
typeof function() {} == "function"
""
string literal ""
字符串文字
typeof "" == "string"
``
string template literal ``
字符串模板文字
typeof `` == "string"
42
number literal 42
数字文字
typeof 42 == "number"
/x/
RegExp literal /x/
RegExp文字
typeof /x/ == "object"
[]
array literal []
数组文字
typeof [] == "object"
false
boolean literal false
布尔文字
typeof false == "boolean"
null
literal for null object, note that null
对象的null
文字,请注意
typeof null == "object"
{}
and object literal {}
和对象文字
typeof {} == "object"
Of all those, only string literals and number literals have value instanceof Object == false
. 在所有这些字符串中,只有字符串文字和数字文字具有
value instanceof Object == false
。 The rest is all instance of object. 其余的都是对象的实例。
The caveats in typeof
and instanceof
are important when writing code that may receive various types. 在编写可能接收各种类型的代码时,
typeof
和instanceof
的警告很重要。 Generally typeof
logic is: 通常,
typeof
逻辑为:
new String
)? new String
)吗? - return "string" undefined
(note that null
is not undefined!) - return "undefined" undefined
(请注意, null
不是未定义的!)-返回“未定义”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.