this

1、this指向

1)作为函数调用,this指向的是window,严格模式下指向underfined

2)在对象中this指向调用的实例对象

3)call, apply中,this指向第一个参数,若为null,指向undefined

2、改变this所属对象方法: call, apply, bind

1)call 与 apply 区别: 一个参数时,两者相同;多个参数时,apply第二个参数传入的是一个数组

var person = {
     name : 'zhansan',
     age : 12
}
var age = 15;                // 等于 window.age  = 15 
function getAge() {        // 等于window.getAge = function () { }
     console.log(this.age) // this 指向 window 对象  
}
getAge()  // 15
getAge.call(person);      //12 , this 指向 person 对象

3、解决方法中覆盖this 问题:

var obj = {
    name:'zdc',
    nums:[1,2,3],
    show: function () {
        var that = this;                                
        this.nums.forEach(function(item){
             console.log(that.name + ":" + item);
       })
   }
}    
var obj = {
    name:'zdc',
    nums:[1,2,3],
    show: function () {                            
        this.nums.forEach(function(item){
             console.log(this.name + ":" + item);
       },this)
   }
}  
var obj = {
    name:'zdc',
    nums:[1,2,3],
    show: function () {                            
        this.nums.forEach(function(item){
             console.log(this.name + ":" + item);
       }).bind(this)
   }
}  
原文地址:https://www.cnblogs.com/yuyedaocao/p/11989536.html