Home  >  Article  >  Web Front-end  >  js变量作用域

js变量作用域

WBOY
WBOYOriginal
2016-06-01 09:55:051078browse

当在函数内部定义变量时,一定要用var定义,否则该变量将是全局变量。

实例:

<code>function fn1{    
   b=2;
   alert(b);
}</code>

此时,如果页面执行了fn1函数,则b变量将成为全局变量。

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn