全局上下文

文章链接

可以通过 this 引用,在浏览器中,全局对象就是 Window 对象。

      console.log(this)
    

全局对象是由 Object 构造函数实例化的一个对象。

      console.log(this instanceof Object)
    

预定义了一堆,嗯,一大堆函数和属性。

      // 都能生效
      console.log(Math.random())
      console.log(this.Math.random())
    

作为全局变量的宿主。

      var a = 'a'
      console.log(this.a)
    

在浏览器 中,全局对象有 window 属性指向自身

      var b = 'b'
      console.log(window.b)

      this.window.c = 'cc'
      console.log(this.c)