this的指向
作者:互联网
eg:
var number = 50; var obj = { number: 60, getNum: function () { var number = 70; return this.number; } }; console.log(obj.getNum());//60 this的指向为obj console.log(obj.getNum.call());//undefined this 指向的是全局对象,实际上就是通过call的首个参数修改了对象函数的this 值 console.log(obj.getNum.call({number:20})); //20 this 指向的是全局对象,后参数定义了全局对象的number
扩展:
var newObj = { number : 666 }
obj.getNum.call(newObj,"22") //666
.call中首参数有值则改变了this指向,此时this指向为newObj,后参数为传参
若首参数无或为null,则this指向全局对象
标签:obj,指向,number,getNum,call,var 来源: https://www.cnblogs.com/redFeather/p/16435372.html