JavaScript apply() 方法
source link: https://www.myfreax.com/javascript-apply-method/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
JavaScript apply() 方法
在本教程中,您将了解类型JavaScript Function
的 apply()
方法以及如何有效地使用它。
JavaScript apply()方法介绍
Function.prototype.apply()
方法允许使用指定的 this
值和一个类数组对象的参数调用函数。下面是 apply()
方法的语法:
fn.apply(thisArg, [args]);
apply()
方法接受两个参数:
thisArg
是为调用 fn 函数提供 this 的值。args
是一个数组参数,指定函数的参数fn
。从 ES5 开始,args
参数可以是类数组对象或数组对象。
apply()
方法与 call()
方法类似,只是它将函数的参数作为数组而不是每个参数都都单独传递。
JavaScript apply() 方法示例
让我们举一些使用 apply()
方法的例子。
简单的 JavaScript apply() 方法示例
假设你有一个person
对象:
const person = {
firstName: 'John',
lastName: 'Doe'
}
和一个命名为 greet()
的函数,如下:
function greet(greeting, message) {
return `${greeting} ${this.firstName}. ${message}`;
}
greet()
函数接受两个参数:greeting
和 message
。在 greet()
函数内部,我们引用了一个对象,该对象有 firstName
属性。
下面的示例展示如何使用 apply()
方法调用 greet()
函数并设置 greet()
函数的 this
值为 person
对象:
let result = greet.apply(person, ['Hello', 'How are you?']);
console.log(result);
Hello John. How are you?
在这个例子中,我们使用 apply()
方法将 greet()
函数内部的 this
值设置为 person
对象。函数的参数作为数组传递给 greet()
方法 。
该apply()
方法调用了greet()
函数,并将this
值设置为person
对象并将参数设置为数组['Hello', 'How are you?']
。
如果使用 call()
方法,则需要单独传递 greet()
函数的参数,如下所示:
let result = greet.call(person, Hello', 'How are you?');
apply()
方法允许一个对象借用另一个对象的方法,在不复制粘帖代码的情况下。
假设您有computer
对象:
const computer = {
name: 'MacBook',
isOn: false,
turnOn() {
this.isOn = true;
return `The ${this.name} is On`;
},
turnOff() {
this.isOn = false;
return `The ${this.name} is Off`;
}
};
和 server
对象:
const server = {
name: 'Dell PowerEdge T30',
isOn: false
};
Code language: JavaScript (javascript)
server
对象没有 turnOn()
和 turnOff()
方法。
要在 server
对象上执行 computer
对象 turnOn()
的方法,可以像下面这样使用 apply()
方法:
let result = computer.turnOn.apply(server);
console.log(result);
The Dell PowerEdge T30 is On
在这个例子中,server
对象借用 computer
对象的 turnOn()
方法。同样,你可以在 server 对象调用 computer 对象的 turnOff() 方法。
let result = computer.turnOff.apply(server);
console.log(result);
The Dell PowerEdge T30 is Off
使用 apply()
方法将一个数组追加到另一个数组
apply()
方法允许您将数组的元素追加到另一个数组:
let arr = [1, 2, 3];
let numbers = [4, 5, 6];
arr.push.apply(arr, numbers);
console.log(arr);
在此示例中,apply()
方法修改原始数组 arr
。值得一提的是,Array.prototype.concat() 方法也有同样的效果,只是它返回新数组而不是修改原始数组。
apply()
方法调用函数使用指定的 this 值和一个类数组对象的参数。apply()
方法与call()
方法类似,只是apply()
接受的参数是数组而不是单个参数
微信公众号
支付宝打赏
Recommend
-
69
I could argue that one of the most misunderstood aspects of JavaScript is the 'this' keyword. In this video, we'll walk through four techniques for figuring out what the 'this' keyword is referencing. They are 1) Implicit Binding 2) Explicit Bin...
-
47
illustration from undraw.co If you are learning JavaScript, you might have seen the this keyword. The
-
144
原文链接:How to use the apply(?), call(?), and bind(➰) methods in JavaScript 原文作者:Ashay Mandwarya 译者:JintNiu 推荐理由:apply,call 和 bind
-
68
You can find previous part of this serieshere. We already know that a function is a special type of object and has several properties, we already discussed it. To better understand what we are going to deal wit...
-
27
Understanding This, Bind, Call, and Apply in JavaScript Sign up for our newsletter. Get the latest tutorials on SysA...
-
26
(给 前端大全 加星标,提升前端技能 ) 作者:达达前端 公号 / Jeskson(本文来自作者投稿) 前言,为什么要学习在掌握JavaScript中的this,call,apply,因为面试官会问...
-
22
插播一条广告
-
11
【优雅代码】深入浅出 妙用Javascript中apply、call、bind所以,可以看出 call 和 apply 是为了动态改变 this 而出现的,当一个 object 没有某个方法(本栗子中banana没有say方法),但是其他的有(本栗子中apple有say方法),我们可以借助call或apply用其它对...
-
12
手写实现call,apply,bind方法实现call方法Function.prototype.myCall = function(thisArg, ...args) { const fn = Symbol('fn') // 声明一...
-
9
JavaScript函数中的三个方法.call(), .apply(), .bind(),总体来说主要功能就是改变函数中 this 关键字的指向,因为 this 默认指向当前环境的对象; ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK