0

While trying to delete a global (window) object, I came across the following behavior, and I was wondering if someone could explain the rationale behind the setting of propertydescriptors.

enter image description here

I don't understand how this makes sense. Thanks.

Firefox 52.0.1 (32-bit) Windows 7.

Erik Bennett
  • 1,049
  • 6
  • 15
  • `var`-declared properties of the global objects are non-configurable, but declarations don't change the attributes of already-existing properties (`window.dog = 1` vs no `window.cat = …`) – Bergi Mar 18 '17 at 22:05
  • 1
    …and had you put all of this in a script, the `var` declarations would have gotten hoisted, so both properties would be non-configurable. – Bergi Mar 18 '17 at 22:07

1 Answers1

2

When you write:

window.dog = 1; 

you are explicitly creating a new property on the global window object called dog. As such, when it gets created, it gets created with its configurable switch set to true. The fact that you then declare

var dog = 6;

doesn't change the fact that the property has already been explicitly created.

When you declare:

var cat = 9;

you are implicitly creating a property called cat on the global window object. Created this way, the property has its configurable switch set to false.

The point being that global properties are generally a bad thing and in the rare occasions when you might want one, it is best to be explicit about it, for readability sake, but also (as you have found out) for the most flexibility in working with them.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • Thanks. I'm no fan of globals. Scope wasn't my issue. My question was about this behavior. This all came about because I was trying to delete a function and found that depending on how it was created, I got different results. So my question remains: What is the rationale behind this? I assume that because there's a difference, there's a reason for it. – Erik Bennett Mar 18 '17 at 22:21
  • I have no idea what you are saying. This is exactly why this happens. I have explained the rationale for it. – Scott Marcus Mar 19 '17 at 02:12
  • So, for globals, a "var" gets placed in the "window" object, and for "var"s inside of a function they get placed... where? – Erik Bennett Mar 19 '17 at 13:05
  • You may have gathered that I'm new to this. I've been reading a lot of docs on JavaScript, many of them conflicting. One of them said that "window.myVar" was the *same* as "var myVar". This does not appear to be altogether correct in that it doesn't generalize to other scopes (function/block). True? Any pointers (books/links) on these matters would help. Thanks. – Erik Bennett Mar 19 '17 at 13:36
  • @ErikBennett That statement is true in terms of scope (accessibility to the variable). As you've seen, it implements the accessibility differently. Since global variables should really be avoided in almost all use cases, this turns out to be largely a non-issue. There are literally thousands of resources out there on learning JavaScript. I would suggest first learning about scope (which is handled differently in JS than in many other languages and gives rise bugs if not understood and to closures, which are powerful, but potentially confusing). – Scott Marcus Mar 19 '17 at 15:01
  • @ErikBennett variables declared with `var` are scoped to their containing function. If they are not contained in a function, then they are scoped to the global object. In a browser, that is the `window` object. – Scott Marcus Mar 19 '17 at 15:03
  • If anyone ever trips over this thread someday, what I was looking for was the "activation object". Thanks to all of the above people who helped me find it. – Erik Bennett Apr 04 '17 at 08:03
  • @Erik Bennett Activation Objects were replaced with Environment Records back in ES5. Read this for details: http://stackoverflow.com/questions/20139050/what-really-is-a-declarative-environment-record-and-how-does-it-differ-from-an-a – Scott Marcus Apr 05 '17 at 15:54
  • Super! +1 for clarification. – Erik Bennett Apr 06 '17 at 04:30