1
//No issues in below:
let a = [1, 2, 3, 4, 5];
for (let i of a) {
document.write('<br />' + i); //1 2 3 4 5
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//No issues in below either
function add(...numbers: number[]): number {
let temp: number = 0;
for (var i: number = 0; i < numbers.length; i++) {
temp += numbers[i];
}
return temp;
}
let result: number = add(1,5,8);
document.body.innerHTML = `Result = ${result}`; //Result = 14
// +++++++++++++++++++++++++++++++++++++++++++++++++
// Hmm! Undefined
function add(...numbers: number[]): number {
let temp: number = 0;
for (let i of numbers) {
temp += numbers[i];
}
return temp;
}
let result: number = add(1,5,8);
document.body.innerHTML = `Result = ${result}`; // Result = NaN
我想在TypeScript中使用新的「of」循環。 但是,沒有按照我的預期工作。例如,在最後一次嘗試中,當我預計得到14時,我得到「NaN」值。 我做錯了什麼?如何在TypeScript中使用for ...循環?
對不起,我在第一次嘗試中增加了下面的內容: –
let result:number = add(1,5,8); document.body.innerHTML ='Result = $ {result}'; //結果= 14 –
難道你不是指'temp + = i;'? –