apply()方法调用一个具有给定this值的函数,以及作为一个数组(或类似数组对象)提供 的参数。
const details = {
message: ‘Hello World!’
};
function getMessage(){
return this. message;
}
getMessage. apply(details); // ‘Hello World!’
call()方法的作用和apply ()方法类似,区别就是call()方法接受的是参数列表,而apply () 方法接受的是一个参数数组。
const person = {
name: "Marko Polo"
};
function greeting(greetingMessage) {
return {greetingMessage} {this, name}
;
}
greeting. apply(person, [‘Hello’]); // "Hello Marko Polo!"
Was this helpful?
0 / 0