Javascript中的局部变量、全局变量的详解与var、let的使用区别
<div id="output_wrapper_id" class="output_wrapper" style="font-size: 15px; color: rgba(62, 62, 62, 1); line-height: 1.8; word-spacing: 2px; letter-spacing: 2px; font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif; background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%); background-size: 20px 20px; background-position: center"><h3 id="h" style="color: inherit; line-height: inherit; padding: 0; margin: 1.6em 0; font-weight: bold; border-bottom: 2px solid rgba(239, 112, 96, 1); font-size: 1.3em"><span style="font-size: inherit; line-height: inherit; margin: 0 3px 0 0; display: inline-block; font-weight: normal; background: rgba(239, 112, 96, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px">前言</span></h3>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">Javascript中的变量定义方式有以下三种方式:<br>1、直接定义变量,var与let均不写;</p>
<pre><code class="javascript language-javascript hljs" style="overflow-wrap: break-word; margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">a = <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(104, 151, 187, 1); word-wrap: inherit !important; word-break: inherit !important">10</span>;<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">2、使用var关键字定义变量</p>
<pre><code class="javascript language-javascript hljs" style="overflow-wrap: break-word; margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">var</span> a = <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(104, 151, 187, 1); word-wrap: inherit !important; word-break: inherit !important">10</span>;<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">3、使用let关键字定义变量</p>
<pre><code class="javascript language-javascript hljs" style="overflow-wrap: break-word; margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">let</span> a = <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(104, 151, 187, 1); word-wrap: inherit !important; word-break: inherit !important">10</span>;<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">这三种方式有什么区别呢?<br>JavaScript全局变量和局部变量又是什么呢?<br>可以带着这两个问题往下看。</p>
<h3 id="h-1" style="color: inherit; line-height: inherit; padding: 0; margin: 1.6em 0; font-weight: bold; border-bottom: 2px solid rgba(239, 112, 96, 1); font-size: 1.3em"><span style="font-size: inherit; line-height: inherit; margin: 0 3px 0 0; display: inline-block; font-weight: normal; background: rgba(239, 112, 96, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px">变量的作用域</span></h3>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">变量是有作用域的,大多数语言中的变量的作用域都有全局变量和局部变量之分。<br>首先我们建立一个文件<strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">test1.html</strong>,从中输入以下代码:</p>
<pre><code class="javascript language-javascript hljs" style="overflow-wrap: break-word; margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><script type=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"text/javascript"</span>><br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//在函数外使用var关键字声明变量test_var1</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">var</span> test_var1 = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"变量1"</span>;<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//定义函数testFun</span><br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">function</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 198, 109, 1); word-wrap: inherit !important; word-break: inherit !important">testFun</span>()</span>{<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//在函数中定义变量没加任何关键字的变量test_var2</span><br> test_var2 = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"变量2"</span>;<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//函数内使用var关键字定义的test_var3</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">var</span> test_var3 = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"变量3"</span>;<br> }<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//函数执行</span><br> testFun();<br> alert(test_var1 + <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"\n"</span> + test_var2);<br> alert(test_var3);<br><span class="xml" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"></<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(232, 191, 106, 1); word-wrap: inherit !important; word-break: inherit !important">script</span>></span></span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">在浏览器打开我们的html页面,可以看到只弹出了一个弹窗:</p>
<img src="https://img-blog.csdnimg.cn/20200220191121643.png">
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">第二个弹窗为什么没有成功弹出呢?<br>我们按F12,看一下报错内容:</p>
<img src="https://img-blog.csdnimg.cn/20200220191717935.png">
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">由此可以看出 <strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">test_var3</strong> 在函数执行后是没有被定义的。说明在函数体内用 <strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">var</strong> 关键字声明的变量 <strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">test_var3</strong> 是局部变量;<br>在函数体外使用 <strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">var</strong> 关键字定义的变量<strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">test_var1</strong> 和在函数体内未用任何关键字定义的变量 <strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">test_var2</strong> 是全局变量。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">得出结论:<br><strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">在函数体外使用var关键字定义的变量和在函数体内未用任何关键字声明的变量是全局变量,在函数体内使用var关键字声明的变量是局部变量。</strong></p>
<h3 id="hvar" style="color: inherit; line-height: inherit; padding: 0; margin: 1.6em 0; font-weight: bold; border-bottom: 2px solid rgba(239, 112, 96, 1); font-size: 1.3em"><span style="font-size: inherit; line-height: inherit; margin: 0 3px 0 0; display: inline-block; font-weight: normal; background: rgba(239, 112, 96, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px">var声明的全局变量和局部变量同名</span></h3>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">我们建立一个文件<strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">test2.html</strong>,从中输入以下代码:</p>
<pre><code class="javascript language-javascript hljs" style="overflow-wrap: break-word; margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><script type=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"text/javascript"</span>><br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//在函数外使用var关键字声明变量test_var</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">var</span> test_var = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"函数体外的变量"</span>;<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//定义函数testFun</span><br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">function</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 198, 109, 1); word-wrap: inherit !important; word-break: inherit !important">testFun</span>()</span>{<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//函数内使用var关键字声明变量test_var</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">var</span> test_var = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"函数体内的变量"</span>;<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//弹窗弹出test_var</span><br> alert(test_var);<br> }<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//函数执行</span><br> testFun();<br> alert(test_var);<br><span class="xml" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"></<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(232, 191, 106, 1); word-wrap: inherit !important; word-break: inherit !important">script</span>></span></span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">打开该文件,会弹出两次弹窗,分别是:</p>
<img src="https://img-blog.csdnimg.cn/20200220192658552.png"><br>点击确定后还会弹窗一次:
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0"> </p>
<img src="https://img-blog.csdnimg.cn/20200220192758296.png">
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">在函数体外定义的全局变量<strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">test_var</strong>,但是在函数体内又定义了局部变量<strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">test_var</strong>,在函数中弹出的<strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">test_var</strong>是函数体内的局部变量覆盖函数体外的全局变量的结果,当离开函数后,局部变量失效,将会看到全局变量。</p>
<h3 id="hjavascript" style="color: inherit; line-height: inherit; padding: 0; margin: 1.6em 0; font-weight: bold; border-bottom: 2px solid rgba(239, 112, 96, 1); font-size: 1.3em"><span style="font-size: inherit; line-height: inherit; margin: 0 3px 0 0; display: inline-block; font-weight: normal; background: rgba(239, 112, 96, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px">JavaScript中的变量有块范围吗?</span></h3>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">在<strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">Java</strong>、<strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">C</strong>、<strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">C++</strong>等语言中,在 <strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">if块</strong> ,<strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">循环块</strong>中定义的变量,出了该块之后将,不能继续访问。那<strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">JavaScript</strong>中是否也如此呢?</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">我们建立一个文件<strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">test3.html</strong>,从中输入以下代码:</p>
<pre><code class="javascript language-javascript hljs" style="overflow-wrap: break-word; margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><script type=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"text/javascript"</span>><br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//定义函数testFun</span><br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">function</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 198, 109, 1); word-wrap: inherit !important; word-break: inherit !important">testFun</span>()</span>{<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//函数内使用var关键字声明变量test_var</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">var</span> test_var1 = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"1"</span>;<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//if代码块</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">if</span>(test_var1 == <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"1"</span>){<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//定义变量test_var2</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">var</span> test_var2 = <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(104, 151, 187, 1); word-wrap: inherit !important; word-break: inherit !important">10</span>;<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//for代码块</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">for</span>(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">var</span> i = <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(104, 151, 187, 1); word-wrap: inherit !important; word-break: inherit !important">0</span>; i < <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(104, 151, 187, 1); word-wrap: inherit !important; word-break: inherit !important">5</span>; i++){<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//打印输出i</span><br> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">document</span>.write(i);<br> }<br> }<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//在if块外访问test_var2</span><br> alert(test_var2);<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//在循环体外访问i</span><br> alert(i);<br> }<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//函数执行</span><br> testFun();<br><span class="xml" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"></<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(232, 191, 106, 1); word-wrap: inherit !important; word-break: inherit !important">script</span>></span></span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">我们可以看到两个弹窗:</p>
<img src="https://img-blog.csdnimg.cn/20200220194148717.png">
<img src="https://img-blog.csdnimg.cn/20200220194201649.png">
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">并且能看到页面上的输出</p>
<img src="https://img-blog.csdnimg.cn/20200220194227342.png">
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">由此我们可以知道:<br><strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">在函数体内中的 if 块和循环体内定义的变量,在函数内都是可以访问的。</strong></p>
<h3 id="h-2" style="color: inherit; line-height: inherit; padding: 0; margin: 1.6em 0; font-weight: bold; border-bottom: 2px solid rgba(239, 112, 96, 1); font-size: 1.3em"><span style="font-size: inherit; line-height: inherit; margin: 0 3px 0 0; display: inline-block; font-weight: normal; background: rgba(239, 112, 96, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px">变量提升</span></h3>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">前面介绍中已经知道:局部变量和全局变量同名时,局部变量会覆盖全局变量。<br>我们定义test4.html,输入以下代码:</p>
<pre><code class="javascript language-javascript hljs" style="overflow-wrap: break-word; margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><script type=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"text/javascript"</span>><br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//在函数外使用var关键字声明变量test_var</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">var</span> test_var = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"函数外的test_var"</span>;<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//定义函数testFun</span><br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">function</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 198, 109, 1); word-wrap: inherit !important; word-break: inherit !important">testFun</span>()</span>{<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//打印输出test_var</span><br> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">document</span>.writeln(test_var + <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"<br>"</span>);<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//函数内使用var关键字定义的test_var</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">var</span> test_var = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"函数内的test_var"</span>;<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//再次打印输出test_var</span><br> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">document</span>.writeln(test_var + <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"<br>"</span>);<br> }<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//函数执行</span><br> testFun();<br><span class="xml" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"></<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(232, 191, 106, 1); word-wrap: inherit !important; word-break: inherit !important">script</span>></span></span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">输出如下:</p>
<img src="https://img-blog.csdnimg.cn/20200220195022374.png">
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">在输出全局变量时,居然输出的是underfined,这是什么情况呢?<br>这便是JavaScript的变量提升机制起了”作用“。下面介绍一下变量提升:<br>在函数体内变量声明总会被解释器”提升“到函数体的顶部,<br>那么上面的代码,会变成如下情况:</p>
<pre><code class="javascript language-javascript hljs" style="overflow-wrap: break-word; margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><script type=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"text/javascript"</span>><br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//在函数外使用var关键字声明变量test_var</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">var</span> test_var = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"函数外的test_var"</span>;<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//定义函数testFun</span><br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">function</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 198, 109, 1); word-wrap: inherit !important; word-break: inherit !important">testFun</span>()</span>{<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//声明被提升到顶部,但是未被赋值</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">var</span> test_var;<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//打印输出test_var</span><br> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">document</span>.writeln(test_var + <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"<br>"</span>);<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//给test_var赋值</span><br> test_var = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"函数内的test_var"</span>;<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//再次打印输出test_var</span><br> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">document</span>.writeln(test_var + <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"<br>"</span>);<br> }<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//函数执行</span><br> testFun();<br><span class="xml" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"></<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(232, 191, 106, 1); word-wrap: inherit !important; word-break: inherit !important">script</span>></span></span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0"><strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">由此可见,变量提升只提升声明部分,不提示赋值部分。</strong></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">我们定义test5.html,输入以下代码:</p>
<pre><code class="javascript language-javascript hljs" style="overflow-wrap: break-word; margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><script type=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"text/javascript"</span>><br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//在函数外使用var关键字声明变量test_var1</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">var</span> test_var1 = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"函数外的test_var1"</span>;<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//在函数外使用var关键字声明变量test_var2</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">var</span> test_var2 = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"函数外的test_var2"</span>;<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//定义函数testFun</span><br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">function</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 198, 109, 1); word-wrap: inherit !important; word-break: inherit !important">testFun</span>()</span>{<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//打印输出test_var1</span><br> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">document</span>.writeln(test_var1 + <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"<br>"</span>);<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//打印输出test_var2</span><br> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">document</span>.writeln(test_var2 + <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"<br>"</span>);<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//for循环的条件为假不会被执行</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">for</span>(;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(104, 151, 187, 1); word-wrap: inherit !important; word-break: inherit !important">-1</span>><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(104, 151, 187, 1); word-wrap: inherit !important; word-break: inherit !important">5</span>;){<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//在函数内使用var关键字声明变量test_var1</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">var</span> test_var1 = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"函数内的test_var1"</span>;<br> }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">return</span>;<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//return后面的语句不会被执行</span><br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//在函数内使用var关键字声明变量test_var2</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">var</span> test_var2 = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"函数内的test_var2"</span>;<br> }<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//函数执行</span><br> testFun();<br><span class="xml" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"></<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(232, 191, 106, 1); word-wrap: inherit !important; word-break: inherit !important">script</span>></span></span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">输出如下:</p>
<img src="https://img-blog.csdnimg.cn/20200220200053781.png"><br>在函数内的<strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">test_var1</strong> 和<strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">test_var2</strong> 的变量定义根本不会被执行,为何还是输出undefined呢?<br>这也是变量提升起的”作用“。
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0"> </p>
<h3 id="hlet" style="color: inherit; line-height: inherit; padding: 0; margin: 1.6em 0; font-weight: bold; border-bottom: 2px solid rgba(239, 112, 96, 1); font-size: 1.3em"><span style="font-size: inherit; line-height: inherit; margin: 0 3px 0 0; display: inline-block; font-weight: normal; background: rgba(239, 112, 96, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px">let关键字定义变量</span></h3>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">从前面我们可以看到,var定义的变量没有块作用域,还有变量提升机制,为了克服这些问题,便引入了<strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">let</strong>关键字。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">我们定义test6.html,输入以下代码:</p>
<pre><code class="javascript language-javascript hljs" style="overflow-wrap: break-word; margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><script type=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"text/javascript"</span>><br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//循环体</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">for</span>(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">let</span> v = <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(104, 151, 187, 1); word-wrap: inherit !important; word-break: inherit !important">0</span>;v < <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(104, 151, 187, 1); word-wrap: inherit !important; word-break: inherit !important">5</span>;v++){<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//在循环体内输出v</span><br> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">console</span>.log(v);<br> }<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//在循环体外输出v</span><br> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">console</span>.log(v);<br><span class="xml" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"></<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(232, 191, 106, 1); word-wrap: inherit !important; word-break: inherit !important">script</span>></span></span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">按F12,打开console,看到输出如下:</p>
<img src="https://img-blog.csdnimg.cn/20200220200839140.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjQzNDcy,size_16,color_FFFFFF,t_70">
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">可以看到在循环体外不能访问循环体内定义的变量。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">我们定义test7.html,输入以下代码:</p>
<pre><code class="javascript language-javascript hljs" style="overflow-wrap: break-word; margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><script type=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"text/javascript"</span>><br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//在函数外使用let关键字声明变量test_var</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">let</span> test_var = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"函数外的test_var"</span>;<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//定义函数testFun</span><br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">function</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 198, 109, 1); word-wrap: inherit !important; word-break: inherit !important">testFun</span>()</span>{<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//打印输出test_var</span><br> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">console</span>.log(test_var);<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//在函数内使用let关键字声明变量test_var</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">let</span> test_var = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(106, 135, 89, 1); word-wrap: inherit !important; word-break: inherit !important">"函数内的test_var"</span><br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//打印输出test_var</span><br> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(204, 120, 50, 1); word-wrap: inherit !important; word-break: inherit !important">console</span>.log(test_var); <br> }<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">//函数执行</span><br> testFun();<br><span class="xml" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"></<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(232, 191, 106, 1); word-wrap: inherit !important; word-break: inherit !important">script</span>></span></span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">按F12,打开console,看到输出如下:</p>
<img src="https://img-blog.csdnimg.cn/20200220201247919.png"><br>这是因为函数内有和全局变量同名的局部变量,会覆盖掉全局变量,但是let关键字声明的变量并没有提升机制,所以会报错。
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0"> </p>
<h3 id="h-3" style="color: inherit; line-height: inherit; padding: 0; margin: 1.6em 0; font-weight: bold; border-bottom: 2px solid rgba(239, 112, 96, 1); font-size: 1.3em"><span style="font-size: inherit; line-height: inherit; margin: 0 3px 0 0; display: inline-block; font-weight: normal; background: rgba(239, 112, 96, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px">小结</span></h3>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">本文介绍了JavaScript中的局部变量和全局变量的知识和var,let声明变量的区别。给我们的启示是如果浏览器支持let关键字,那么就尽量用let来避免变量提升机制等情况。</p>
<h3 id="h-4" style="color: inherit; line-height: inherit; padding: 0; margin: 1.6em 0; font-weight: bold; border-bottom: 2px solid rgba(239, 112, 96, 1); font-size: 1.3em"><span style="font-size: inherit; line-height: inherit; margin: 0 3px 0 0; display: inline-block; font-weight: normal; background: rgba(239, 112, 96, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px">欢迎关注</span></h3>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.7em 0">扫下方二维码即可关注,微信公众号:<strong style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold; color: rgba(233, 105, 0, 1)">code随笔</strong></p>
<img src="https://img-blog.csdnimg.cn/20200125160744348.png"></div><br><br>
来源:https://www.cnblogs.com/nicaicai/p/12337317.html
頁:
[1]