Daniel Thompson
Daniel Thompson

Reputation: 2351

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?

Upvotes: 7

Views: 2764

Answers (3)

Alexis Wilke
Alexis Wilke

Reputation: 20720

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 vim 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.

Upvotes: 5

Raja
Raja

Reputation: 1259

Officially 'status' may be defined as a keyword or not; but I landed in trouble when I tried to use it as an identifier. Here is a snippet:

<html>
<body>
status IS a key word.
<br/>
<span id="stat1"></span></div>
<br/>
<span id="stat2"></span></div>
  <script>
    var status = document.getElementById("stat1");
    status.innerHTML = "foo";
    console.log(status.innerHTML);
    var xtatus = document.getElementById("stat2");
    xtatus.innerHTML = "bar";
    console.log(xtatus.innerHTML);
  </script>
</body>
</html>

The content is not displayed until you agree to change the name. The console shows 'undefined' instead of 'foo'.

Upvotes: 0

Jonathan Mongeau
Jonathan Mongeau

Reputation: 122

If you play around in your console. You can do the following:

-> status
<- ""

-> window.status
<- ""

-> status='333'
<- "333"

-> status
<- "333"

-> window.status
<- "333"

This to me indicates that the keyword status is simply an alias for the window.status property. What exactly window.status does I am not sure.

EDIT: After reading the comment below, I realized that properties of the windows object are essentially global. So this makes status the same as window.status and NOT an alias as I mention above.

See this Stack Overflow about the window object: Is window really global in Javascript?

Upvotes: 2

Related Questions