apply ()方法可以在使用一个指定的this值和一个参数数组(或类数组对象)的前提下调用 某个函数或方法。call()方法类似于apply(),不同之处仅仅是call()接受的参数是参数列表。

const obj1 = {
    result:0
};
const obj2 = {
    result:0
};
function reduceAdd(){
    let result = 0;
    for(let i = 0, len = arguments.length; i < len; i++) {
        result += arguments[i];
    }
    this. resuIt = result;
}
reduceAdd.apply(obj1, [1, 2t 3, 4, 5]); // 15
reduceAdd.call(obj2, 1, 2, 3, 4, 5); // 15

Was this helpful?

0 / 0

发表回复 0

Your email address will not be published.