## this的指向取决是该函数被调用是的对象。函数
function test(){ var name=123; console.log(this.name); console.log(this); } test() ; //<empty string> window window.test() ; //<empty string> window
#### window 是js的全局对象。
#### 总结1:若是函数有this,可是没有被上一级对象调用,这个时候window做为全局对象,在非严格模式下,this指向的就是window。this
var o={ name:123, test:function(){ console.log(this.name); } } o.test() ; //123
#### 总结2:若是函数中有this,这个函数被上一级对象所调用,这是this,就是指向上一级对象。code
var o= { name:123, type:'boy', f: { name:456, test:function(){ console.log(this.name); //456 console.log(this.type); //undefined } } }; o.f.test() ;
#### 总结3:若是这个函数包含多个对象,尽管该函数被最外层对象调用,this指向的也是上一级对象。对象