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

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

@@ -4,7 +4,7 @@
<div><iframe class="interactive interactive-js" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/object-freeze.html" width="100%"></iframe></div>
<p class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a class="external" href="https://github.com/mdn/interactive-examples" rel="noopener">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox"><code>Object.freeze(<em>obj</em>)</code></pre>
<pre><code class="language-javascript"><code>Object.freeze(<em>obj</em>)</code></code></pre>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
<dt><code>obj</code></dt>
@@ -18,7 +18,7 @@
<p>这个方法返回传递的对象,而不是创建一个被冻结的副本。</p>
<h2 id="Examples" name="Examples">例子</h2>
<h3 id="冻结对象">冻结对象</h3>
<pre class="brush: js">var obj = {
<pre><code class="language-javascript">var obj = {
prop: function() {},
foo: 'bar'
};
@@ -59,9 +59,9 @@ Object.defineProperty(obj, 'foo', { value: 'eit' });
// 也不能更改原型
// 下面两个语句都会抛出 TypeError.
Object.setPrototypeOf(obj, { x: 20 })
obj.__proto__ = { x: 20 }</pre>
obj.__proto__ = { x: 20 }</code></pre>
<h3 class="brush: js" id="冻结数组">冻结数组</h3>
<pre class="brush: js">let a = [0];
<pre><code class="language-javascript">let a = [0];
Object.freeze(a); // 现在数组不能被修改了.
a[0]=1; // fails silently
@@ -74,9 +74,9 @@ function fail() {
a.push(2);
}
fail();</pre>
fail();</code></pre>
<p>被冻结的对象是不可变的。但也不总是这样。下例展示了冻结对象不是常量对象(浅冻结)。</p>
<pre class="brush: js">obj1 = {
<pre><code class="language-javascript">obj1 = {
internal: {}
};
@@ -84,10 +84,10 @@ Object.freeze(obj1);
obj1.internal.a = 'aValue';
obj1.internal.a // 'aValue'
</pre>
</code></pre>
<p>对于一个常量对象,整个引用图(直接和间接引用其他对象)只能引用不可变的冻结对象。冻结的对象被认为是不可变的,因为整个对象中的整个对象状态(对其他对象的值和引用)是固定的。注意,字符串,数字和布尔总是不可变的,而函数和数组是对象。</p>
<p>要使对象不可变,需要递归冻结每个类型为对象的属性(深冻结)。当你知道对象在引用图中不包含任何 <em><a class="external" href="https://en.wikipedia.org/wiki/Cycle_(graph_theory)" rel="noopener"></a> </em>(循环引用)时,将根据你的设计逐个使用该模式,否则将触发无限循环。对 deepFreeze()  的增强将是具有接收路径例如Array参数的内部函数以便当对象进入不变时可以递归地调用 deepFreeze() 。你仍然有冻结不应冻结的对象的风险,例如[window]。</p>
<pre class="brush: js">// 深冻结函数.
<pre><code class="language-javascript">// 深冻结函数.
function deepFreeze(obj) {
// 取回定义在obj上的属性名
@@ -113,14 +113,14 @@ obj2 = {
deepFreeze(obj2);
obj2.internal.a = 'anotherValue';
obj2.internal.a; // undefined
</pre>
</code></pre>
<h2 id="Notes">Notes</h2>
<p>在ES5中如果这个方法的参数不是一个对象一个原始值那么它会导致 <a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a>。在ES2015中非对象参数将被视为要被冻结的普通对象并被简单地返回。</p>
<pre class="brush: js">&gt; Object.freeze(1)
<pre><code class="language-javascript">&gt; Object.freeze(1)
TypeError: 1 is not an object // ES5 code
&gt; Object.freeze(1)
1 // ES2015 code</pre>
1 // ES2015 code</code></pre>
<h3 id="对比_Object.seal()">对比 <code>Object.seal()</code></h3>
<p><code>Object.seal()</code>密封的对象可以改变它们现有的属性。使用<code>Object.freeze()</code> 冻结的对象中现有属性是不可变的。</p>
<h2 id="规范" style="margin-bottom: 20px; line-height: 30px;">规范</h2>