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

3

7

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?

Wonacott answered 6/4, 2018 at 2:50 Comment(4)
status is not a javascript keyword as far as I've been able to find - where does your information come from? the status in window.status does not make status a keyword ... it is a property of window - see window.status "documentation" - perhaps you are thinking of static?Blanche
I'm not seeing it in the specification: ecma-international.org/ecma-262/8.0/…Lulualaba
"I just noticed that status is a keyword in JavaScript" I don't know where you got that information because it absolutely is not.Stagestruck
Ahh I see, let me reword my question, it was foolish to assume it was a keyword just because of my syntax highlighting.Wonacott
T
5

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.

Thaddeus answered 16/12, 2018 at 2:47 Comment(2)
The linked Mozilla documentation doesn't contain the status keyword (including the version at the time of this answer) 🤔Wellgroomed
@AdamMillerchip You are right. My answer is wrong. I think I was mistaken as there is a static reserved keyword. status was just a global variable.Thaddeus
I
2

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?

Ileus answered 6/4, 2018 at 2:57 Comment(3)
it's not an alias ... window.status === status ... it's as much an alias as setTimout is for window.setTimeout - i.e, it's not an alias at allBlanche
window-ception :pBlanche
Appreciate the answer Jonathan, I guess my syntax highlighting is geared for browser JavaScript then?Wonacott
M
0

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

Monreal answered 5/10 at 5:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.