简体   繁体   English

javascript中有多少个文字?

[英]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语言中的integerfloat等。

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 literalfunction literal
  • javascript object literal is object literal javascript object literalobject 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 下面是

  • NullLiteral ::== null NullLiteral :: == null
  • BooleanLiteral ::== true | BooleanLiteral :: == true | false
  • NumberLiteral 数字字面量
  • RegularExpressionLiteral 正则表达式
  • StringLiteral 字符串字面量
  • TemplateLiteral components. TemplateLiteral组件。

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 ),您会看到诸如ObjectLiteralArrayLiteral之类的术语,但它们也被称为{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. 在编写可能接收各种类型的代码时, typeofinstanceof的警告很重要。 Generally typeof logic is: 通常, typeof逻辑为:

  • Is it a raw string (not new String )? 它是原始字符串(不是new String )吗? - return "string" -返回“字符串”
  • Is it a raw number? 这是原始数字吗? - return "number" -返回“数字”
  • Is it raw boolean? 它是原始的布尔值吗? - return "boolean" -返回“布尔值”
  • Is it undefined (note that null is not undefined!) - return "undefined" 它是undefined (请注意, null不是未定义的!)-返回“未定义”
  • Is it a function? 是功能吗? - return "function" -返回“功能”
  • Otherwise return "object" 否则返回“对象”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM