一、call()方法
call是在函数后面调用的,可以忽略函数的this指向
语法:函数名.call(this指向,参数1,参数2...)
var obj = { name: 'Jack' }
function fn(a, b) {
console.log(this)
console.log(a)
console.log(b)
}
fn(1, 2)//this指向wind
fn.call(obj, 1, 2)//this指向obj
一、apply()方法
apply是在函数后面调用的,可以忽略函数的this指向
语法:函数名.apply(this指向 , [参数1,参数2...])
var obj = { name: 'Jack' }
function fn(a, b) {
console.log(this)
console.log(a)
console.log(b)
}
fn(1, 2)//this指向wind
fn.apply(obj, [1, 2])//this指向obj
一、bind()方法
call是在函数后面调用的,可以忽略函数的this指向
语法:var newFn = 函数名.bind(this指向)
var obj = { name: 'Jack' }
function fn(a, b) {
console.log(this)
console.log(a)
console.log(b)
}
fn(1, 2)//this指向wind
var newFn = fn.bind(obj)
newFn(1, 2)//this指向obj