this关键字
=> 表示当前对象
=> 1. 对象
2. 根据当前使用场景决定
=>事件处理函数
this表示事件源对象
=>普通函数
this指向 window对象
=>自调用函数this -> window对象
=> 定时器this -> window对象
=> 对象方法中this -> 调用方法的引用变量指向的对象
var btnEle = document.querySelector('button')
btnEle.addEventListener('click',function(){
console.log(this); //this表示事件源对象
})
button
// var obj = {
// name:'jack',
// age:22,
// say:function(){
// console.log('对象方法中this ->',this);
// }
// }
// obj.say()
this表示整个对象
改变整个指向
<!--
函数()
函数.call(this指向新对象)
传参
函数.apply(this指向新对象)
函数.bind(this指向新对象)
=>返回改this指向后的新函数
-->
var obj = {name:'jack'}
function fn(a,b){
console.log('fn this -> ',this);
console.log('a : ',a ,' b :',b);
}
fn(10,20)
fn.call(obj,10,20)
fn this -> Window
a : 10 b : 20
fn this -> Object name: "jack"
a : 10 b : 20
var obj = {name:'jack'}
function fn(a,b){
console.log('fn this -> ',this);
console.log('a : ',a ,' b :',b);
}
fn(10,20)
fn.apply(obj,[10,20])
fn this -> Window
a : 10 b : 20
fn this -> {name: 'jack'}
a : 10 b : 20
var obj = {name:'jack'}
function fn(a,b){
console.log('fn this -> ',this);
console.log('a : ',a ,' b :',b);
}
var newFn = fn.bind(obj)
newFn(10,20)
fn this -> {name: 'jack'}
13改变this指向.html:29 a : 10 b : 20