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

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

@@ -3,10 +3,10 @@
<div></div>
<div><strong>with语句 </strong>扩展一个语句的作用域链。</div>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox">with (expression) {
<pre><code class="language-javascript">with (expression) {
<em>statement</em>
}
</pre>
</code></pre>
<dl>
<dt><code>expression</code></dt>
<dd>将给定的表达式添加到在评估语句时使用的作用域链上。表达式周围的括号是必需的。</dd>
@@ -21,32 +21,32 @@
<p><strong>弊:</strong><code>with</code>语句使得程序在查找变量值时,都是先在指定的对象中查找。所以那些本来不是这个对象的属性的变量,查找起来将会很慢。如果是在对性能要求较高的场合,'with'下面的statement语句中的变量只应该包含这个指定对象的属性。</p>
<h3 id="语义不明的弊端">语义不明的弊端</h3>
<p><strong>弊端:</strong><code>with</code>语句使得代码不易阅读同时使得JavaScript编译器难以在作用域链上查找某个变量难以决定应该在哪个对象上来取值。请看下面的例子</p>
<pre class="brush: js">function f(x, o) {
<pre><code class="language-javascript">function f(x, o) {
with (o)
print(x);
}</pre>
}</code></pre>
<p><code>f</code>被调用时,<code>x</code>有可能能取到值,也可能是<code>undefined</code>,如果能取到, 有可能是在o上取的值也可能是函数的第一个参数<code>x</code>的值如果o中没有这个属性的话。如果你忘记在作为第二个参数的对象o中定义<code>x</code>这个属性,程序并不会报错,只是取到另一个值而已。</p>
<p><strong>弊端:</strong>使用<code>with</code>语句的代码,无法向前兼容,特別是在使用一些原生数据类型的时候。看下面的例子:</p>
<div>
<pre class="brush:js">function f(foo, values) {
<pre><code class="language-js">function f(foo, values) {
with (foo) {
console.log(values)
}
}
</pre>
</code></pre>
<p><font face="Open Sans, Arial, sans-serif">如果是在</font>ECMAScript 5环境调用<code>f([1,2,3], obj)</code>,则<code><font face="Open Sans, Arial, sans-serif">with</font></code>语句中变量<code>values</code>将指向函数的第二个参数<code>values</code>。但是ECMAScript 6标准给<code><a href="Reference/Global_Objects/Array/prototype">Array.prototype</a></code>添加了一个新属性<code>values</code>所有数组实例将继承这个属性。所以在ECMAScript 6环境中<code><font face="Open Sans, Arial, sans-serif">with</font></code>语句中变量<code>values</code>将指向<code>[1,2,3].values</code></p>
</div>
<h2 id="Examples" name="Examples">示例</h2>
<h3 id="Example:_Using_with" name="Example:_Using_with">Example: Using <code>with</code></h3>
<p>下面的<code>with</code>语句指定<code><a href="Reference/Global_Objects/Math" title="JavaScript/Reference/Global_Objects/Math">Math</a></code>对象作为默认对象。<code>with</code>语句里面的变量,分別指向<code>Math</code>对象的<a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/PI" title="JavaScript/Reference/Global_Objects/Math/PI"><code>PI</code></a> 、<code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cos" title="JavaScript/Reference/Global_Objects/Math/cos">cos</a><font face="Open Sans, Arial, sans-serif"></font></code><code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sin" title="JavaScript/Reference/Global_Objects/Math/sin">sin</a></code>函数,不用在前面添加命名空间。</p>
<pre class="brush:js">var a, x, y;
<pre><code class="language-js">var a, x, y;
var r = 10;
with (Math) {
a = PI * r * r;
x = r * cos(PI);
y = r * sin(PI / 2);
}</pre>
}</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>