2 回答

TA貢獻(xiàn)1998條經(jīng)驗 獲得超6個贊
您沒有使用函數(shù)參數(shù),而是定義了新變量。這會起作用:
function move(parameter, parameter2) {
var storage = parameter.pop();
parameter2.push(storage);
}
let anArray = [1, 2];
let anotherArray = [3, 4];
move(anArray, anotherArray)
console.log(anArray)
console.log(anotherArray)

TA貢獻(xiàn)1815條經(jīng)驗 獲得超10個贊
因為在move你定義的函數(shù)anArray中anotherArray。外部數(shù)組的范圍與內(nèi)部定義的變量的范圍不同。實際上,移動發(fā)生在方法內(nèi)部定義的數(shù)組中。由于您已經(jīng)使用相同的名稱定義了它們,因此會造成混淆。
請參閱下文,了解您所做的實際工作但不在傳遞的參數(shù)上的實現(xiàn)
function move(parameter, parameter2) {
var anArray = [1, 2];
var anotherArray = [3, 4];
var storage = anArray.pop();
anotherArray.push(storage);
console.log(anArray)
console.log(anotherArray)
}
move([],[])
因此,為了使您的函數(shù)在您傳遞的輸入?yún)?shù)上工作,您實際上可以進(jìn)行如下更改
function move(parameter, parameter2) {
const storage = parameter.pop();
parameter2.push(storage);
}
let anArray = [1, 2];
let anotherArray = [3, 4];
move(anArray, anotherArray)
console.log(anArray)
console.log(anotherArray)
希望這可以幫助。
添加回答
舉報