ES6

js中的call方法的使用

2021-12-17  本文已影响0人  Frank_Fang

调用某个对象中的函数方法,在另一个对象上使用它

const person = {
  fullName: function (city, country) {
    return `${this.firstName} ${this.lastName}, ${city}, ${country}`
  }
}
const person1 = {
  firstName: "Bill",
  lastName: "Gates"
}
const person2 = {
  firstName: "Steve",
  lastName: "Jobs"
}

// 调用person对象中的fullName方法,在person2上使用它
const personInfo = person.fullName.call(person2, "Seatle", "USA");
console.log(personInfo) //Steve Jobs, Seatle, USA
上一篇 下一篇

猜你喜欢

热点阅读