Home > Web Front-end > JS Tutorial > body text

javascript定义变量时带var与不带var的区别分析_javascript技巧

WBOY
Release: 2016-05-16 16:20:56
Original
1197 people have browsed it

本文实例分析了javascript定义变量时带var与不带var的区别。分享给大家供大家参考。具体分析如下:

直接看实例里说明:

复制代码 代码如下:

严格来说:函数体内不带var,并不是指定义一个变量,而是进行变量赋值,即var abc;abc=8。在函数体内如果进行赋值 abc=80(不带var),实际过程是这样的——该语句先在函数体内查找变量abc,如果找不到,它会往上在函数体外继续查找变量abc,如果还是找不到,最后没有办法,只能在函数体外定义变量var abc。

所以,为什么

复制代码 代码如下:
function test(){
 abc = 80;
}

能够在函数外部直接调用变量abc,也就是这么一个原因。

希望本文所述对大家的javascript程序设计有所帮助。

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!