【译】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


相关文章
|
12月前
|
JavaScript 前端开发
js中的bind,call,apply方法的区别以及用法
JavaScript中,`bind`、`call`和`apply`均可改变函数的`this`指向并传递参数。其中,`bind`返回一个新函数,不立即执行;`call`和`apply`则立即执行,且`apply`的参数以数组形式传递。三者在改变`this`指向及传参上功能相似,但在执行时机和参数传递方式上有所区别。
212 1
|
JavaScript 前端开发
JS高级—call(),apply(),bind()
【10月更文挑战第17天】call()`、`apply()`和`bind()`是 JavaScript 中非常重要的工具,它们为我们提供了灵活控制函数执行和`this`指向的能力。通过合理运用这些方法,可以实现更复杂的编程逻辑和功能,提升代码的质量和可维护性。你在实际开发中可以根据具体需求,选择合适的方法来满足业务需求,并不断探索它们的更多应用场景。
136 1
|
JavaScript 前端开发
js 中call()和apply()
js 中call()和apply()
94 1
|
自然语言处理 JavaScript 前端开发
JS中this的应用场景,再了解下apply、call和bind!
该文章深入探讨了JavaScript中`this`关键字的多种应用场景,并详细解释了`apply`、`call`和`bind`这三个函数方法的使用技巧和差异。
|
JavaScript 前端开发
JavaScript深入之bind的模拟实现
JavaScript深入系列第十一篇,通过bind函数的模拟实现,带大家真正了解bind的特性
243 0
JavaScript深入之bind的模拟实现
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的客户关系管理系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的客户关系管理系统附带文章源码部署视频讲解等
280 2
|
12月前
|
JavaScript 前端开发
JavaScript中的原型 保姆级文章一文搞懂
本文详细解析了JavaScript中的原型概念,从构造函数、原型对象、`__proto__`属性、`constructor`属性到原型链,层层递进地解释了JavaScript如何通过原型实现继承机制。适合初学者深入理解JS面向对象编程的核心原理。
210 1
JavaScript中的原型 保姆级文章一文搞懂
|
12月前
JS+CSS3文章内容背景黑白切换源码
JS+CSS3文章内容背景黑白切换源码是一款基于JS+CSS3制作的简单网页文章文字内容背景颜色黑白切换效果。
132 0
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的小区物流配送系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的小区物流配送系统附带文章源码部署视频讲解等
418 5
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的家政平台附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的家政平台附带文章源码部署视频讲解等
244 4

热门文章

最新文章