aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/object/preventextensions/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/object/preventextensions/index.html')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/object/preventextensions/index.html131
1 files changed, 131 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/object/preventextensions/index.html b/files/zh-cn/web/javascript/reference/global_objects/object/preventextensions/index.html
new file mode 100644
index 0000000000..81c8a451a2
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/object/preventextensions/index.html
@@ -0,0 +1,131 @@
+---
+title: Object.preventExtensions()
+slug: Web/JavaScript/Reference/Global_Objects/Object/preventExtensions
+tags:
+ - ECMAScript 5
+ - JavaScript
+ - Method
+ - Object
+translation_of: Web/JavaScript/Reference/Global_Objects/Object/preventExtensions
+---
+<div>{{JSRef("Global_Objects", "Object")}}</div>
+
+<p><code><strong>Object.preventExtensions()</strong></code>方法让一个对象变的不可扩展,也就是永远不能再添加新的属性。</p>
+
+<p>{{EmbedInteractiveExample("pages/js/object-preventextensions.html")}}</p>
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre class="syntaxbox"><code>Object.preventExtensions(<em>obj</em>)</code></pre>
+
+<h3 id="Parameters" name="Parameters">参数</h3>
+
+<dl>
+ <dt><code>obj</code></dt>
+ <dd>将要变得不可扩展的对象。</dd>
+ <dt>
+ <h3 id="返回值">返回值</h3>
+ </dt>
+ <dd>已经不可扩展的对象。</dd>
+</dl>
+
+<h2 id="Description" name="Description">描述</h2>
+
+<p>如果一个对象可以添加新的属性,则这个对象是可扩展的。<code>Object.preventExtensions()</code>将对象标记为不再可扩展,这样它将永远不会具有它被标记为不可扩展时持有的属性之外的属性。注意,一般来说,不可扩展对象的属性可能仍然可被<em>删除</em>。尝试将新属性添加到不可扩展对象将静默失败或抛出{{jsxref("TypeError")}}(最常见的情况是{{jsxref("Functions_and_function_scope/Strict_mode", "strict mode", "", 1)}}中,但不排除其他情况)。</p>
+
+<p><code>Object.preventExtensions()</code>仅阻止添加自身的属性。但其对象类型的原型依然可以添加新的属性。</p>
+
+<p>该方法使得目标对象的 <code>[[prototype]]</code>  不可变;任何重新赋值 <code>[[prototype]]</code> 操作都会抛出 <code>TypeError</code> 。这种行为只针对内部的 <code>[[prototype]]</code> 属性, 目标对象的其它属性将保持可变。</p>
+
+<p>一旦将对象变为不可扩展的对象,就再也不能使其可扩展。</p>
+
+<h2 id="Examples" name="Examples">例子</h2>
+
+<pre class="brush: js">// Object.preventExtensions将原对象变的不可扩展,并且返回原对象.
+var obj = {};
+var obj2 = Object.preventExtensions(obj);
+obj === obj2; // true
+
+// 字面量方式定义的对象默认是可扩展的.
+var empty = {};
+Object.isExtensible(empty) //=== true
+
+// ...但可以改变.
+Object.preventExtensions(empty);
+Object.isExtensible(empty) //=== false
+
+// 使用Object.defineProperty方法为一个不可扩展的对象添加新属性会抛出异常.
+var nonExtensible = { removable: true };
+Object.preventExtensions(nonExtensible);
+Object.defineProperty(nonExtensible, "new", { value: 8675309 }); // 抛出TypeError异常
+
+// 在严格模式中,为一个不可扩展对象的新属性赋值会抛出TypeError异常.
+function fail()
+{
+ "use strict";
+ nonExtensible.newProperty = "FAIL"; // throws a TypeError
+}
+fail();
+</pre>
+
+<p>​​​​</p>
+
+<p>不可扩展对象的原型是不可变的:</p>
+
+<pre>var fixed = Object.preventExtensions({});
+// throws a 'TypeError'.
+fixed.__proto__ = { oh: 'hai' };</pre>
+
+<h2 id="Notes">Notes</h2>
+
+<p>在 ES5 中,如果参数不是一个对象类型(而是原始类型),将抛出一个{{jsxref("TypeError")}}异常。在 ES2015 中,非对象参数将被视为一个不可扩展的普通对象,因此会被直接返回。</p>
+
+<pre class="brush: js">Object.preventExtensions(1);
+// TypeError: 1 is not an object (ES5 code)
+
+Object.preventExtensions(1);
+// 1 (ES2015 code)</pre>
+
+<h2 id="规范" style="margin-bottom: 20px; line-height: 30px;">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-15.2.3.10', 'Object.preventExtensions')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td>Initial definition. Implemented in JavaScript 1.8.5.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-object.preventextensions', 'Object.preventExtensions')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td></td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-object.preventextensions', 'Object.preventExtensions')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容">浏览器兼容</h2>
+
+<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
+
+<p>{{Compat("javascript.builtins.Object.preventExtensions")}}</p>
+
+<h2 id="See_also" name="See_also">相关链接</h2>
+
+<ul>
+ <li>{{jsxref("Object.isExtensible()")}}</li>
+ <li>{{jsxref("Object.seal()")}}</li>
+ <li>{{jsxref("Object.isSealed()")}}</li>
+ <li>{{jsxref("Object.freeze()")}}</li>
+ <li>{{jsxref("Object.isFrozen()")}}</li>
+ <li>{{jsxref("Reflect.preventExtensions()")}}</li>
+</ul>