简体   繁体   中英

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:

/ab+c/g

I know that in programming languages there are some fixed list of possible literals, like in C language integer , float etc.

Then I searched for the list of literals supported in javascript but could not found satisfactory answer.

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
  • template literal is essentially string literal
  • function literal is function literal
  • javascript object literal is 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 .

Where is it written? How can I find out list of possible literals in 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 .

Also relevant is 11.8 Literals . Thereunder are

  • NullLiteral ::== null
  • BooleanLiteral ::== true | false
  • NumberLiteral
  • RegularExpressionLiteral
  • StringLiteral
  • TemplateLiteral components.

Notably, undefined is not a literal.

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.

You might avoid thinking too hard about typeof results. 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.

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
    • typeof ()=>{} == "function"
  • function() {} function literal
    • typeof function() {} == "function"
  • "" string literal
    • typeof "" == "string"
  • `` string template literal
    • typeof `` == "string"
  • 42 number literal
    • typeof 42 == "number"
  • /x/ RegExp literal
    • typeof /x/ == "object"
  • [] array literal
    • typeof [] == "object"
  • false boolean literal
    • typeof false == "boolean"
  • null literal for null object, note that
    • typeof null == "object"
  • {} and object literal
    • typeof {} == "object"

Of all those, only string literals and number literals have 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. Generally typeof logic is:

  • Is it a raw string (not 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"
  • Is it a function? - return "function"
  • Otherwise return "object"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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