0

It is added to one of window's properties when you define global variable.

var foo = 'foo';

console.log(window.foo);
//result : 'foo'

As you know, The scope of the JavaScript variable is function.

For global variables to be stored, I must use a function because of scope.

But it is not a global variable already if I wrap the variable by a function.

You can't help but add to window's properties in my opinion.

However, the guess is just a guess.

I've always wondered why it is happened.

But I don't still know why.

Is there anyone who know why?

liam
  • 23
  • 8
  • *"As you know, The scope of the JavaScript variable is function"* this is something *you* are assuming. – Federico klez Culloca Nov 28 '18 at 08:29
  • 6
    Possible duplicate of [Are global variables just properties on the \`window\` object?](https://stackoverflow.com/questions/19855823/are-global-variables-just-properties-on-the-window-object) – Mav Nov 28 '18 at 08:30
  • https://stackoverflow.com/questions/19855823/are-global-variables-just-properties-on-the-window-object – Mav Nov 28 '18 at 08:31
  • *Because the spec says so, because that's the way Brendan wrote it…* – deceze Nov 28 '18 at 08:36
  • @liam if you think the duplicate answers your question, then yes. It's not mandatory but it would help – Federico klez Culloca Nov 28 '18 at 08:42

0 Answers0