const Olympics = {
event: "羽球",
player: "小戴"
}
function play(result, review) {
console.log(`${this.event}比賽結果: ${this.player}${result},${review}`)
}
// 寫法1:把引數寫在 bind 後面
const badmintonResult = play.bind(Olympics, "贏了", "好棒")
badmintonResult()
// 印出:羽球比賽結果: 小戴贏了,好棒
// 寫法2:把引數寫在已宣告的物件上
const badmintonResult = play.bind(Olympics)
badmintonResult("贏了", "好棒")
// 印出:羽球比賽結果: 小戴贏了,好棒
const Olympics = {
event: "羽球",
player: "小戴"
}
function play(result, review) {
console.log(`${this.event}比賽結果: ${this.player}${result},${review}`)
}
play.call(Olympics, "贏了", "好棒")
// 印出:羽球比賽結果: 小戴贏了,好棒
const Olympics = {
event: "羽球",
player: "小戴"
}
function play(result, review) {
console.log(`${this.event}比賽結果: ${this.player}${result},${review}`)
}
play.apply(Olympics, ["贏了", "好棒"])
// 印出:羽球比賽結果: 小戴贏了,好棒
參考:
[1]Function.prototype.bind()
[2]Function.prototype.call
[3]Function.prototype.apply()