跳到主要内容

call,apply&bind

call,apply&bind

1. call

var foo = {
value: 1,
}

function bar(arg1, arg2) {
console.log(this.value)
console.log(arg1, arg2)
}

bar.call(foo, 1, 2) // 1

2. apply

var foo = {
value: 1,
}

function bar(arg1, arg2) {
console.log(this.value)
console.log(arg1, arg2)
}

bar.apply(foo, [1, 2]) // 1

3. bind

var foo = {
value: 1,
}

function bar(arg1, arg2) {
console.log(this.value)
console.log(arg1, arg2)
}

var bindFoo = bar.bind(foo, 1, 2)
bindFoo() // 1

4. 总结

总结