我完全理解在JS上使用.apply()方法的基本原理。
function sayHi(x, y, z) {
console.log("I'm " + this.name + ", My choices are" + x + "," + y + "," + z);
}
var personA = {
name : "Albert"
};
var personB = {
name : "Bob"
};
var name = "Cindy";
var options = [" a", " b", " c"];
sayHi.apply(personA, options); // "I'm Albert, My choices are a, b, c"
sayHi.apply(personB, [" d", " e", " f"]); // "I'm Bob, My choices are d, e, f"

以上代码段是理想情况,您只想在sayHi()
函数中使用一定数量的数组元素并传递< strong> sayHi()
函数所需的数据(数组)的完全相同数量。
然而,有时您需要在给定数据集中使用数组元素的全部,其中数据也可能是不同的数量。 因此,sayHi()
函数不仅可以接受包含3个元素的数组,而且可以接受4,5,6或更多元素的数组,而不仅仅是接受它,还要利用传递的所有元素,像这样的东西:
function sayHi(....) {
// Accept any array with varying amount of elements, and using it
for (...) {
// A loop that will go through each element in given array
}
}
var personA = {
name : "Albert"
};
var personB = {
name : "Bob"
};
var name = "Cindy";
var options3 = [" a", " b", " c"];
var options4 = [" a", " b", " c", " d"];
var options5 = [" a", " b", " c", " d", " e"];
// and so on
sayHi.apply(personA, options3);
sayHi.apply(personA, options4);
sayHi.apply(personA, options5);
到目前为止,我还没有找到办法,所以我需要你的帮助向我解释。
谢谢,
答案 0 :(得分:2)
您可以使用3种不同的方法。
首先,您可以使用arguments
类似数组的对象,而不是git remote set-url origin --push https://url1.com/a.git https://url2.com/a.git
,x
和y
。
z
function sayHi() {
for(var choice in arguments){
// Do stuff.
}
}
&#13;
这种方法的缺点是function sayHi() {
// Note that arguments is not an array but only an array-like object.
var choicesStr = "My choices are " + Array.prototype.join.call(arguments, ", ");
console.log("I'm " + this.name + ", " + choicesStr);
}
var personA = {
name : "Albert"
};
var personB = {
name : "Bob"
};
var name = "Cindy";
var options = ["a", "b", "c"];
sayHi.apply(personA, options); // "I'm Albert, My choices are a, b, c"
sayHi.apply(personB, ["d", "e", "f", "i", "j"]); // "I'm Bob, My choices are d, e, f"
将包含每个参数。如果您需要提供应该区别对待的第一个参数,则必须先将其解压缩。
其次,您可以使用ES6 rest parameter功能。
arguments
function sayHi(...choices) {
for(var choice in choices){
// Do stuff.
}
}
&#13;
优点是可以轻松提供将单独考虑的航向参数。
function sayHi(...choices) {
// Note that unlike arguments, the rest parameter is a real array.
var choicesStr = "My choices are " + choices.join(", ");
console.log("I'm " + this.name + ", " + choicesStr);
}
var personA = {
name : "Albert"
};
var personB = {
name : "Bob"
};
var name = "Cindy";
var options = ["a", "b", "c"];
sayHi.apply(personA, options); // "I'm Albert, My choices are a, b, c"
sayHi.apply(personB, ["d", " e", " f", "i", "j"]); // "I'm Bob, My choices are d, e, f"
这种方法的缺点是its support is not yet widespread。如果这是一个问题,您可以使用Babel之类的转发器。
传递一个数组
function sayHi(firstArg, ...choices) {
for(var choice in choices){
// Do stuff.
}
}
&#13;
答案 1 :(得分:0)
您希望使用函数的arguments
对象,该对象为您提供传入的所有参数的数组,无论是否已命名。
function sayHi(){
for (var i=0;i<arguments.length;++i){
console.log( "Hi "+arguments[i] );
}
}
var parents = ["Mom","Dad"];
var children= ["Suzie","Jacob","Jerome","Sandra"];
sayHi.apply(this,parents);
sayHi.apply(this,children);
&#13;
但是,请注意,如果您要将一组对象传递给一个函数,您也可以传入数组,而不是跳过apply
和{{ 1}}:
arguments
&#13;