语法高亮,滚动条美化,设置页面调整

This commit is contained in:
fofolee
2019-04-19 02:41:09 +08:00
parent 1e8f76c000
commit 359d29ee0b
1590 changed files with 12328 additions and 11441 deletions

View File

@@ -29,21 +29,21 @@
</div>
</div>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox"><code>undefined </code></pre>
<pre><code class="language-javascript"><code>undefined </code></code></pre>
<h2 id="Description" name="Description">描述</h2>
<p><code>undefined</code>是全局对象的一个属性。也就是说,它是全局作用域的一个变量。<code>undefined</code>的最初值就是原始数据类型<code><a class="glossaryLink" href="/en-US/docs/Glossary/Undefined" title="undefined: A primitive value automatically assigned to variables that have just been declared or to formal arguments for which there are no actual arguments.">undefined</a></code></p>
<p>在现代浏览器JavaScript 1.8.5/Firefox 4+自ECMAscript5标准以来undefined是一个不能被配置non-configurable不能被重写non-writable的属性。即便事实并非如此也要避免去重写它。</p>
<p>一个没有被赋值的变量的类型是undefined。如果方法或者是语句中<strong>操作的变量没有被赋值则会返回undefined</strong>(对于这句话持疑惑态度,请查看英文原文来理解)。</p>
<pre class="brush: js">function test(a){
<pre><code class="language-javascript">function test(a){
console.log(typeof a); // undefined
return a;
}
test(); // 返回"undefined"</pre>
test(); // 返回"undefined"</code></pre>
<p>一个函数如果没有使用return语句指定<a href="Reference/Statements/return" title="return语句终止函数的执行并返回一个指定的值给函数调用者。"><code>返回</code></a>就会返回一个undefined值。</p>
<div class="warning">
<p>但是它有可能在非全局作用域中被当作<a class="glossaryLink" href="/en-US/docs/Glossary/Identifier" title="标识符: A sequence of characters in the code that identifies a variable, function, or property.">标识符</a>(变量名)来使用(因为undefined不是一个<a href="Reference/Reserved_Words" title="此页面仍未被本地化, 期待您的翻译!"><code>保留字</code></a>)),这样做是一个非常坏的主意,因为这样会使你的代码难以去维护和排错。</p>
<pre class="brush: js">// 不要这样做!
<pre><code class="language-javascript">// 不要这样做!
// 打印 'foo string' PS说明undefined的值和类型都已经改变
(function() {
@@ -56,40 +56,40 @@ console.log(undefined, typeof undefined)
console.log(undefined, typeof undefined)
})('foo')
</pre>
</code></pre>
</div>
<h2 id="示例">示例</h2>
<h3 id="严格相等和undefined">严格相等和undefined</h3>
<p>你可以使用undefined和严格相等或不相等操作符来决定一个变量是否拥有值。在下面的代码中变量x是未定义的if 语句的求值结果将是true</p>
<pre class="brush: js">var x;
<pre><code class="language-javascript">var x;
if (x === undefined) {
// 执行这些语句
} else {
// 这些语句不会被执行
}</pre>
}</code></pre>
<div class="note">
<p>注意:这里是必须使用严格相等操作符(===)而不是标准相等操作符(==),因为 x == undefined 会检查x是不是null但是严格相等不会检查。null不等同于undefined。移步<a href="Reference/Operators/Comparison_Operators" title="JavaScript 有两种比较方式:严格比较运算符和转换类型比较运算符。对于严格比较运算符(===)来说,仅当两个操作数的类型相同且值相等为 true而对于被广泛使用的比较运算符==)来说,会在进行比较之前,将两个操作数转换成相同的类型。对于关系运算符(比如 &lt;=)来说,会先将操作数转为原始值,使它们类型相同,再进行比较运算。"><code>比较操作符</code></a>查看详情。</p>
</div>
<h3 id="Typeof_操作符和undefined">Typeof 操作符和undefined</h3>
<p>或者,可以使用<a href="Reference/Operators/typeof" title="typeof操作符返回一个字符串表示未经计算的操作数的类型。"><code>typeof</code></a></p>
<pre class="brush: js">var x;
<pre><code class="language-javascript">var x;
if(typeof x === 'undefined') {
// 执行这些语句
}</pre>
}</code></pre>
<p>使用 <a href="Reference/Operators/typeof" title="typeof操作符返回一个字符串表示未经计算的操作数的类型。"><code>typeof</code></a>的原因是它不会在一个变量没有被声明的时候抛出一个错误。</p>
<pre class="brush: js">// 这里没有声明y
<pre><code class="language-javascript">// 这里没有声明y
if(typeof y === 'undefined') { // 没有错误执行结果为true
console.log("y is " + typeof y ) // y is undefined
}
if(y === undefined) { // ReferenceError: y is not defined
}</pre>
}</code></pre>
<p>但是技术方面看来这样的使用方法应该被避免。JavaScript是一个静态作用域语言所以一个变量是否被声明可以通过看它是否在一个封闭的上下文中被声明。唯一的例外是全局作用域但是全局作用域是被绑定在全局对象上的所以要检查一个变量是否在全局上下文中存在可以通过检查全局对象上是否存在这个属性比如使用<a href="Reference/Operators/in" title="如果指定的属性在指定的对象或其原型链中则in 运算符返回true。"><code>in</code></a>操作符)。</p>
<h3 id="Void操作符和undefined">Void操作符和undefined</h3>
<p><a href="Reference/Operators/void" title="void 运算符 对给定的表达式进行求值,然后返回 undefined。"><code>void</code></a> 操作符是第三种可以替代的方法。</p>
<pre class="brush: js">var x;
<pre><code class="language-javascript">var x;
if(x === void 0) {
// 执行这些语句
}
@@ -98,7 +98,7 @@ if(x === void 0) {
if(y === void 0) {
// 抛出一个RenferenceError错误(与`typeof`相比)
}
</pre>
</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>