爲什麼我必須在下面的例子中使用rest參數和spread運算符?
function f(...y){
y.forEach(v => {
console.log("111");
console.log(v);
});
}
var z=["a","b","c","d","e","f"];
f(z);
預期輸出:
"111"
"a"
"111"
"b"
"111"
"c"
"111"
"d"
"111"
"e"
"111"
"f"
實際輸出:
"111"
["a", "b", "c", "d", "e", "f"]
只有當我改變f(z)
到f(...z)
我得到的預期輸出線。我是ECMAScript 2015的新手。請告訴我在這裏錯過了什麼。
休息參數是沒有必要返回預期的結果,你可以用'函數f(Y ){}' – guest271314