2017-10-17 205 views
-2

我有一個變量在每個函數結束時上升,函數調用變量。問題是,變量不會增加。有什麼建議?變量不在函數中增加

var i; 
i=0; 

example function() { 
<function> 
i++; 
} 

的功能不斷地返回1

+2

這不是有效的sintax,你要麼有sintax錯誤或功能不被稱爲 – juvian

+0

您可能需要閱讀HTTP: //eloquentjavascript.net/03_functions.html來學習關於函數的基礎知識。 –

回答

1

您的代碼有幾個問題:

  • 你沒有正確的聲明你的函數 - 應該說的function example() {代替example function() {
  • <function>無效javascript
  • 你永遠不會調用你的函數,所以它不會執行。

下面是一個重寫版本的作品:

var i; 
 
i=0; 
 

 
function example() { 
 
    i++; 
 
} 
 

 
example(); //execute the function 
 
console.log(i); //i should now be 1 
 
example(); //execute the function again 
 
console.log(i); //i should now be 2 
 
example(); //execute the function again 
 
console.log(i); //i should now be 3

+0

噢,即時通訊對不起,我正在使用它作爲佔位符,所有這些都只是一個例子。 –

+0

實際的函數語法很好。函數x(){i ++;}和僅用於函數的代碼。 –

+0

對不起,我誤解了!通常,在編寫示例代碼時,我會使用類似'// body body goes here'的註釋。 –