1
function User(name,age){
    this.name = name;
    this.age = age;
}
var user = new User('Daniel', 45);
document.write(user[name] + ' is ' + user[age] + ' years old!');

I know the difference between dot notation and bracket notation. When the above code is run in chrome console, Uncaught ReferenceError: age is not defined is shown. Since I've not declared name, it should throw error as name is not defined as it is encountered first in the code. I logged window.name and window.age only to see "" and undefined respectively.

Thinking I might have used name in global scope before, I tested the same code after clearing browser history and also in a new browser, Firefox which I've never used before. The same error is shown in both scenarios.

Does this mean that name is initialized to empty string by default in any browser?

After I did delete window.name;, it gave error on name. But why would name be already empty string in a completely new browser. Any comments would be helpful. Thanks.

  • 'I logged window.name and window.age only to see "" and undefined respectively' - and that didn't ring a bell? ["All String and Symbol values, including the empty string, are valid as property keys"](https://tc39.github.io/ecma262/#sec-object-type). – ASDFGerte Jun 09 '18 at 17:18
  • This not a duplicate of "JavaScript property access: dot notation cs. brackets". Please read the question: "Does this mean that name is initialized to empty string by default in any browser?" Voting to reopen. – Ivan Jun 09 '18 at 17:22
  • @Ivan this is a "get up from your pc, walk for a bit, come back, look at it again, problem (hopefully) solved". Also towards the question, really, all i do is google too - give it a try: [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Window/name). PS: i also tried on Node 10.0.0 and as expected, `window.name` is `undefined` there. – ASDFGerte Jun 09 '18 at 17:25
  • Indeed but this is not a duplicate of the post provided by @Nina. – Ivan Jun 09 '18 at 17:29
  • 1
    @Ivan Take this as unrelated to this topic: i know some questions get closed as duplicate of something vaguely related when it takes too long to get the close votes for something else. What is related is that i don't see much value in reopening it. OLD: "As OP didn't comment, i assume he/she either found the solution by themself or read my (hopefully not inacceptably offending) comments and understood those." NEW: oops, i just remembered commenting takes 15 rep or something - a questionable rule. – ASDFGerte Jun 09 '18 at 17:39
  • see here http://pythontutor.com/javascript.html#code=function%20User%28name,age%29%7B%0A%20%20%20%20this.name%20%3D%20name%3B%0A%20%20%20%20this.age%20%3D%20age%3B%0A%7D%0Avar%20user%20%3D%20new%20User%28'Daniel',%2045%29%3B%0Aconsole.log%28user%5Bname%5D%20%2B%20'%20is%20'%20%2B%20user%5Bage%5D%20%2B%20'%20years%20old!'%29%3B&curInstr=5&mode=display&origin=opt-frontend.js&py=js&rawInputLstJSON=%5B%5D which is showing reference error first name or age? – A l w a y s S u n n y Jun 09 '18 at 17:47
  • I still don't think it would be marked as duplicate: the thread "var name and window.name" is about global variables. I'm still not convinced. – Ivan Jun 09 '18 at 17:52
  • @BeingSunny, Error is thrown on name in pythontutor. My question is why would name be already defined and initialized to empty string in a new browser? – lifetimeLearner007 Jun 09 '18 at 17:53
  • 1
    please see the actual duplicate targets. name is [`window.name`](https://developer.mozilla.org/en-US/docs/Web/API/Window/name) and is a reserved property of window. – Nina Scholz Jun 09 '18 at 17:55
  • `window.name` is the name of the current window: "*The name of the window is used primarily for setting targets for hyperlinks and forms. Windows do not need to have names.*" from [MDN webdocs](https://developer.mozilla.org/en-US/docs/Web/API/Window/name). So in our case it doesn't have a name: `window.name` returns a empty `String`. – Ivan Jun 09 '18 at 17:56

0 Answers0