【译】JavaScript中的call,apply,bind

简介: 【译】JavaScript中的call,apply,bind

在我们开始研究call, apply, bind之前,应该对how does "this" keyword works in JavaScript有所认知。


简言之,"this" 创造了指向一个对象的引用。它可能指向了全局对象,比如在全局作用域{window object}


console.log(this);
//Window {parent: Window, opener: null, top: Window, length: 4, frames: Window, …}
复制代码


"this"在一个对象的内部指向对象的本身。


const student = {
  name: "ritik",
  getDetails(){
    console.log(this)
  }
}
student.getDetails();
// {name: "ritik", getDetails: f}
复制代码


所以,那就是"this"如何通过作用域自动地获取它的上下文的。


但是,如果我们想指定"this"上下文(环境)到一个特定的对象呢?


我们在代码中演示this


const religion = {
  type: "humanity",
  property: "greatest"
}
function getDetails(){
  console.log(`${this.type} is the ${this.property} religion`);
}
getDetails()
// undefined is the undefined religion
复制代码


所以,这里的"this"指向"window"对象(根据函数中"this"默认行为,它指向"window"对象)。


但是,我们想它指向"religion"对象。


那就是call, apply, bind出现的地方。


const religion = {
  type: "humanity",
  property: "greatest"
}
function getDetails(){
  console.log(`${this.type} is the ${this.property} religion`);
}
getDetails.call(religion);
// humanity is the greatest religion
getDetails.apply(religion);
// humanity is the greatest religion
复制代码


这里,"call"或者"apply"方法将"religion"对象和getDetails函数关联起来。


或者,我们可以说,"call"或者"apply"方法在getDetails函数中创造了一个"this"指向"religion"对象。


"call""apply"达到的效果是一样的,但是,它们处理参数的方式不同。


现在,我们传递一些参数给getDetails函数。


const religion = {
  type: "humanity",
  property:"greatest"
}
function getDetails(world,creature){
  console.log(`${this.type} is the ${this.property} religion in the ${world} of ${creature}`);
}
getDetails.call(religion,"modern world","human");
//humanity is the greatest religion in the modern world of human
getDetails.apply(religion,["modern world","human"]);
//humanity is the greatest religion in the modern world of human
复制代码


"call"方法接收以逗号分隔的参数;但是"apply"通过一个数组来处理参数。


现在,如果你想在代码很多地方使用带不同参数的"getDetails"函数。


多次使用"call""apply"是一种解决方法,但是"bind"函数可以让这个过程更加容易。


"bind"方法创造了一个指向传入对象的"this"引用,这和"apply"或者"call"那样,但是其返回一个函数。


现在,在你的代码中,这个函数通过不同参数被多次使用。


const religion = {
  type: "humanity",
  property:"greatest"
}
function getDetails(world,creature){
  console.log(`${this.type} is the ${this.property} religion in the ${world} of ${creature}`);
}
const newgetDetails = getDetails.bind(religion);
newgetDetails("modern world","human");
//humanity is the greatest religion in the modern world of human
newgetDetails("future world","different creatures");
//humanity is the greatest religion in the future world of different creatures
复制代码


如果你不想存储返回的函数,那么它也可以直接引用,如下:


const religion = {
  type: "humanity",
  property:"greatest"
}
function getDetails(world,creature){
  console.log(`${this.type} is the ${this.property} religion in the ${world} of ${creature}`);
}
getDetails.bind(religion)("modern world","human")
//humanity is the greatest religion in the modern world of human


相关文章
|
JavaScript 前端开发
js中的bind,call,apply方法的区别以及用法
JavaScript中,`bind`、`call`和`apply`均可改变函数的`this`指向并传递参数。其中,`bind`返回一个新函数,不立即执行;`call`和`apply`则立即执行,且`apply`的参数以数组形式传递。三者在改变`this`指向及传参上功能相似,但在执行时机和参数传递方式上有所区别。
314 1
|
JavaScript 前端开发
JS高级—call(),apply(),bind()
【10月更文挑战第17天】call()`、`apply()`和`bind()`是 JavaScript 中非常重要的工具,它们为我们提供了灵活控制函数执行和`this`指向的能力。通过合理运用这些方法,可以实现更复杂的编程逻辑和功能,提升代码的质量和可维护性。你在实际开发中可以根据具体需求,选择合适的方法来满足业务需求,并不断探索它们的更多应用场景。
181 1
|
JavaScript 前端开发
js 中call()和apply()
js 中call()和apply()
118 1
|
自然语言处理 JavaScript 前端开发
JS中this的应用场景,再了解下apply、call和bind!
该文章深入探讨了JavaScript中`this`关键字的多种应用场景,并详细解释了`apply`、`call`和`bind`这三个函数方法的使用技巧和差异。
|
JavaScript 前端开发
JavaScript中call()与apply()的作用与区别?
JavaScript中call()与apply()的作用与区别?
|
JavaScript 前端开发
JavaScript call、apply 和 bind 的区别
JavaScript call、apply 和 bind 的区别
249 0
|
前端开发 JavaScript
【Web 前端】 js中call、apply、bind有什么区别?
【4月更文挑战第22天】【Web 前端】 js中call、apply、bind有什么区别?
【Web 前端】 js中call、apply、bind有什么区别?
|
JavaScript 前端开发
JavaScript中call()与apply()的作用与区别?
JavaScript中call()与apply()的作用与区别?
339 3
|
JavaScript 前端开发 开发者
【JavaScript】JavaScript中call、apply与bind的区别:进阶特性与应用场景
【JavaScript】JavaScript中call、apply与bind的区别:进阶特性与应用场景
367 0
|
JavaScript 前端开发
js中call 和 apply的区别
js中call 和 apply的区别
223 0