关于this 对象
this 对象是在运行时基于函数的执行环境绑定的
(1)在全局函数中,this等于window或者global
(2)当函数被作为某个对象的方法调用时,this等于那个对象。
在闭包中使用this对象可能会导致一些问题。因为匿名函数的执行环境具有全局性,因此其this对象通常只想window
(特例,函数通过call( ), apply( )改变函数执行环境的情况下,this会指向会改变)
浏览器中:

node环境中

在浏览器中,当前页面 在外层的变量会添加到window 中,所以第一个例子会返回lmlc
在node环境中,声明赋值的变量不会添加到global中,但一个变量不声明直接初始化,会添加到global环境中。这点需要注意。
每个函数在被调用的时都会自动取得两个特殊变量,this和arguments,内部函数在搜索这两个变量时,只会搜索到其活动对象,不会去搜索外部函数中的这两个变量。不过,可以把外部作用域中的this对象保存在一个闭包能够访问到的变量里,就可以让闭包访问该对象。

this 的值可能会意外发生改变

第三种会输出 lmlc 具体原因待后期分析
网友评论