ReferenceError and the global object

前端 未结 3 1967
滥情空心
滥情空心 2020-11-29 10:56

In javascript window is the global object, which means every object in the global scope is a child of window. So why I get this resul

3条回答
  •  爱一瞬间的悲伤
    2020-11-29 11:10

    In your first example (window.foo) you are accessing a property of the window object. JavaScript returns "undefined" for when you are trying to access a non existent property of a object. It's designed that way.

    In the second example you are referencing a variable directly, and since it does not exists an error is raised.

    It's just the way JavaScript is designed and works.

提交回复
热议问题