2017-10-17 206 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'的注释。 –