一道考察this指向和js运行原理细节的单例模式测试题
原题: var n = 2 ; var obj = { n : 30 , fn : ( function ( n ) { n *= 2 ; this . n += 2 ; var n = 5 ; return function ( m ) { this . n *= 2 ; console . log ( m + ( ++ n ) ) ; } } ) ( n ) } ; var fn = obj . fn ; fn ( 3 ) ; obj . fn ( 3 ) ; console . log ( n , obj . n ) ; 带解析的代码js var n = 2 ; var obj = { n : 30 , fn : ( function ( n ) { n *= 2 ; this . n += 2 ; //this.n =>window.n=2 +=2=4 window.n=4 //console.log(this.n); var n = 5 ; //n在这重新定义赋值5 return function ( m ) { //console.log(m);//3 //console.log(n);//6 在16行被定义成5,因为下面有个++n所以+1 => 6 // console.log(this.n == window.n)//true this . n *= 2 ; /