該函數來自於書籍「Eloquent Javascript」,第3章,函數。Javascript用於循環的公共語言翻譯
我不明白腳本的行爲。
這裏是一個「循環」一個不可能的任務,使計數值等於0和小於0
當時我認爲這將阻止該程序的開始,但是程序很聰明。它如何跨越這個循環。 這裏是片段:
var power = function (base, exponent) {
var result = 1;
for(var count = 0; count < exponent; count ++) {
//repeat as many as needed the self multiplication.
console.log("count = ", count);
result *= base;
console.log(result);
}
return result;
};
console.log("finally we get the number ", power(2, 0));
/* A little question with exponent = 0
Why is that not an error. It is impossible to initiate
with count = 0 && count < exponent.
Weird.*/
在repl.it輸出
Native Browser JavaScript
finally we get the number 1
即使程序知道這是不可能的,所以它跳過for循環並返回'1',這是for循環之前的init。 – fuyushimoya
@fuyushimoya當然,但有可能瞭解如何? – kouty
for循環的條件部分是'count
fuyushimoya