aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/object/seal/index.html
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/object/seal/index.html
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/object/seal/index.html')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/object/seal/index.html151
1 files changed, 151 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/object/seal/index.html b/files/zh-cn/web/javascript/reference/global_objects/object/seal/index.html
new file mode 100644
index 0000000000..7a18ee2e91
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/object/seal/index.html
@@ -0,0 +1,151 @@
+---
+title: Object.seal()
+slug: Web/JavaScript/Reference/Global_Objects/Object/seal
+tags:
+ - ECMAScript 5
+ - JavaScript
+ - Method
+ - Object
+translation_of: Web/JavaScript/Reference/Global_Objects/Object/seal
+---
+<div>{{JSRef}}</div>
+
+<div><code><strong>Object.seal()</strong></code>方法封闭一个对象,阻止添加新属性并将所有现有属性标记为不可配置。当前属性的值只要原来是可写的就可以改变。</div>
+
+<div></div>
+
+<div>{{EmbedInteractiveExample("pages/js/object-prototype-seal.html")}}</div>
+
+
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre class="syntaxbox"><code>Object.seal(<em>obj</em>)</code></pre>
+
+<h3 id="Parameters" name="Parameters">参数</h3>
+
+<dl>
+ <dt><code>obj</code></dt>
+ <dd>将要被密封的对象。</dd>
+</dl>
+
+<h3 id="返回值">返回值</h3>
+
+<p>被密封的对象。</p>
+
+<h2 id="Description" name="Description">描述</h2>
+
+<p>通常,一个对象是{{jsxref("Object.isExtensible()", "可扩展的", "", 1)}}(可以添加新的属性)。密封一个对象会让这个对象变的不能添加新属性,且所有已有属性会变的不可配置。属性不可配置的效果就是属性变的不可删除,以及一个数据属性不能被重新定义成为访问器属性,或者反之。但属性的值仍然可以修改。尝试删除一个密封对象的属性或者将某个密封对象的属性从数据属性转换成访问器属性,结果会静默失败或抛出{{jsxref("TypeError")}}(在{{jsxref("Strict_mode", "严格模式", "", 1)}} 中最常见的,但不唯一)。</p>
+
+<p>不会影响从原型链上继承的属性。但 {{jsxref("Object.proto", "__proto__")}} ( {{deprecated_inline}} ) 属性的值也会不能修改。</p>
+
+<p>返回被密封对象的引用。</p>
+
+<h2 id="Examples" name="Examples">例子</h2>
+
+<pre class="brush: js">var obj = {
+ prop: function() {},
+ foo: 'bar'
+};
+
+// 可以添加新的属性
+// 可以更改或删除现有的属性
+obj.foo = 'baz';
+obj.lumpy = 'woof';
+delete obj.prop;
+
+var o = Object.seal(obj);
+
+o === obj; // true
+Object.isSealed(obj); // === true
+
+// 仍然可以修改密封对象的属性值
+obj.foo = 'quux';
+
+
+// 但是你不能将属性重新定义成为访问器属性
+// 反之亦然
+Object.defineProperty(obj, 'foo', {
+ get: function() { return 'g'; }
+}); // throws a TypeError
+
+// 除了属性值以外的任何变化,都会失败.
+obj.quaxxor = 'the friendly duck';
+// 添加属性将会失败
+delete obj.foo;
+// 删除属性将会失败
+
+// 在严格模式下,这样的尝试将会抛出错误
+function fail() {
+ 'use strict';
+ delete obj.foo; // throws a TypeError
+ obj.sparky = 'arf'; // throws a TypeError
+}
+fail();
+
+// 通过Object.defineProperty添加属性将会报错
+Object.defineProperty(obj, 'ohai', {
+ value: 17
+}); // throws a TypeError
+Object.defineProperty(obj, 'foo', {
+ value: 'eit'
+}); // 通过Object.defineProperty修改属性值</pre>
+
+<h2 id="注意">注意</h2>
+
+<p>在ES5中,如果这个方法的参数不是一个(原始)对象,那么它将导致{{jsxref("TypeError")}}。在ES2015中,非对象参数将被视为已被密封的普通对象,会直接返回它。</p>
+
+<pre class="brush: js">Object.seal(1);
+// TypeError: 1 is not an object (ES5 code)
+
+Object.seal(1);
+// 1 (ES2015 code)</pre>
+
+<h3 id="对比_Object.freeze">对比 <code>Object.freeze()</code></h3>
+
+<p>使用<code>Object.freeze()</code>冻结的对象中的现有属性值是不可变的。用<code>Object.seal()</code>密封的对象可以改变其现有属性值。</p>
+
+<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.8', 'Object.seal')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td>Initial definition. Implemented in JavaScript 1.8.5.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-object.seal', 'Object.seal')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td></td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-object.seal', 'Object.seal')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容</h2>
+
+<div>
+
+
+<p>{{Compat("javascript.builtins.Object.seal")}}</p>
+</div>
+
+<h2 id="See_also" name="See_also">相关链接</h2>
+
+<ul>
+ <li>{{jsxref("Object.isSealed()")}}</li>
+ <li>{{jsxref("Object.preventExtensions()")}}</li>
+ <li>{{jsxref("Object.isExtensible()")}}</li>
+ <li>{{jsxref("Object.freeze()")}}</li>
+ <li>{{jsxref("Object.isFrozen()")}}</li>
+</ul>