Why do variables in the global scope get assigned to the window object?

前端 未结 4 2059
天命终不由人
天命终不由人 2021-01-14 14:53
var foo = \'bar\';
console.log(window.foo); // bar

Seems like variables get assigned as properties to this, but inside anonymous funct

4条回答
  •  一个人的身影
    2021-01-14 15:44

    They're available only in the function they're declared in.

    Function scope is the only other scope in JavaScript, btw, unlike block-scoping in other {} languages.)

    Re: your edit Don't be fooled--JS's this semantics are a bit irksome IMO--this may not be what you expect under a variety of circumstances.

提交回复
热议问题