call apply bind 改变this 指向问题

    技术2022-07-14  109

    call方法 function info(name, age) { this.name = name; this.age = age; } function people(name, age, work) { info.call(this, name, age); console.log("他叫" + this.name + "," + this.age + "岁,职业是" + work) } people("Li", 21, "IT"); apply方法 function info(name, age) { this.name = name; this.age = age; } function people(name, age, work) { info.apply(this, [name, age]); console.log("他叫" + this.name + "," + this.age + "岁,职业是" + work) } people("Zhang", 22, "IT"); bind方法 不带参使用 let info = { name: "Wang" } function people() { console.log("他叫" + this.name) } let func = people.bind(info); func(); bind方法 带参数使用 let info = { name: "Wang" } function people(m, n, o) { console.log(m, n, o) } let func = people.bind(null, '11');; func("22", "33", "44");

    相同点

    1.都能改变this指向

    不同点

    1.bind 调用执行 call apply立即执行

    2.call, bind 依次传入 apply 数组传入

    Processed: 0.010, SQL: 9