Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript syntax highlighting -- is status a keyword? -- what's it used for?

My vim syntax highlighting just lead me to believe that status is a keyword in JavaScript.

Searching around all I can find are articles about window.status in browser JavaScript. Is this the meaning of this 'keyword' status or is there something different going on?

What is the keyword status?

like image 714
Daniel Thompson Avatar asked Mar 07 '23 12:03

Daniel Thompson


1 Answers

This answer is actually incorrect. I probably confounded static with status. The Mozilla website has a page about the window.status. It may have been done that way so you do not try to use that name as a variable. That way you would not inadvertently update the status bar of your browser. The feature doesn't work anymore, but I guess the in editors is lagging.


In the Mozilla documentation (which is easier to read than the ECMA reference,) we find the status keyword under the Future reserved keywords section.

So, it is viewed as a keyword.

However, JavaScript accepts reserved keywords in various places such as after a period as in:

a = {}
a.default = 123
a.status = 555

Here I set the default and status members of object a even though these two names are viewed as reserved keywords in the language.

Actually, if you have been using Promise objects, you may have noticed the catch keyword used as one of the possible callbacks:

Promise.all([a, b, c])
       .then(...)
       .catch(...)     <-- this is a reserved keyword
       .finally(...)   <-- this is a reserved keyword

Here are the pertinent grammar entries:

Identifier :
    IdentifierName but not ReservedWord

MemberExpression :
    PrimaryExpression
    MemberExpression [ Expression ]
    MemberExpression . IdentifierName     <-- look at this one
    MemberExpression TemplateLiteral
    SuperProperty
    MetaProperty
    new MemberExpression Arguments

An IdentifierName is any identifier (more or less [A-Z_$][A-Z_0-9$]*, plus all Unicode characters... they actually follow the Unicode definition of an identifier.) That includes reserved keywords.

As we can see, you are not supposed to start an expression with a ReserverWord, except for a new exception like new and super (not shown here, see SuperProperty.)

So in strict mode (i.e. in a node module) you should get an error if you write:

status = 123

In non-strict mode, status is not a reserved keyword and therefore it is allowed.

One way to make sure that it works when you access variable members is to use the array syntax. For example:

a['default'] = 123
a['status'] = 555

Also that way the names do not get highlighted as reserved keywords by your editor.

like image 52
Alexis Wilke Avatar answered Mar 25 '23 22:03

Alexis Wilke