diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/object/issealed/index.html | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-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/issealed/index.html')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/object/issealed/index.html | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/object/issealed/index.html b/files/zh-cn/web/javascript/reference/global_objects/object/issealed/index.html new file mode 100644 index 0000000000..df91848da8 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/object/issealed/index.html @@ -0,0 +1,124 @@ +--- +title: Object.isSealed() +slug: Web/JavaScript/Reference/Global_Objects/Object/isSealed +tags: + - ECMAScript5 + - JavaScript + - Method + - Object +translation_of: Web/JavaScript/Reference/Global_Objects/Object/isSealed +--- +<div>{{JSRef}}</div> + +<p><strong><code>Object.isSealed()</code></strong> 方法判断一个对象是否被密封。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate"><code>Object.isSealed(<em>obj</em>)</code></pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>obj</code></dt> + <dd>要被检查的对象。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>表示给定对象是否被密封的一个{{jsxref("Boolean")}} 。</p> + +<h2 id="描述">描述</h2> + +<p>如果这个对象是密封的,则返回 <code>true</code>,否则返回 <code>false</code>。密封对象是指那些不可 {{jsxref("Object.isExtensible", "扩展")}} 的,且所有自身属性都不可配置且因此不可删除(但不一定是不可写)的对象。</p> + +<h2 id="Examples" name="Examples">例子</h2> + +<pre class="brush: js notranslate">// 新建的对象默认不是密封的. +var empty = {}; +Object.isSealed(empty); // === false + +// 如果你把一个空对象变的不可扩展,则它同时也会变成个密封对象. +Object.preventExtensions(empty); +Object.isSealed(empty); // === true + +// 但如果这个对象不是空对象,则它不会变成密封对象,因为密封对象的所有自身属性必须是不可配置的. +var hasProp = { fee: "fie foe fum" }; +Object.preventExtensions(hasProp); +Object.isSealed(hasProp); // === false + +// 如果把这个属性变的不可配置,则这个属性也就成了密封对象. +Object.defineProperty(hasProp, "fee", { configurable: false }); +Object.isSealed(hasProp); // === false +Object.isSealed(hasProp.fee); // === true + +// 最简单的方法来生成一个密封对象,当然是使用Object.seal. +var sealed = {}; +Object.seal(sealed); +Object.isSealed(sealed); // === true + +// 一个密封对象同时也是不可扩展的. +Object.isExtensible(sealed); // === false + +// 一个密封对象也可以是一个冻结对象,但不是必须的. +Object.isFrozen(sealed); // === true ,所有的属性都是不可写的 +var s2 = Object.seal({ p: 3 }); +Object.isFrozen(s2); // === false, 属性"p"可写 + +var s3 = Object.seal({ get p() { return 0; } }); +Object.isFrozen(s3); // === true ,访问器属性不考虑可写不可写,只考虑是否可配置</pre> + +<h2 id="注意" style="margin-bottom: 20px; line-height: 30px;">注意</h2> + +<p>在ES5中,如果这个方法的参数不是一个对象(一个原始类型),那么它会导致{{jsxref("TypeError")}}。在ES2015中,非对象参数将被视为是一个密封的普通对象,只返回<code>true</code>。</p> + +<pre class="brush: js notranslate">Object.isSealed(1); +// TypeError: 1 is not an object (ES5 code) + +Object.isSealed(1); +// true (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.11', 'Object.isSealed')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td>Initial definition.<br> + Implemented in JavaScript 1.8.5</td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-object.issealed', 'Object.isSealed')}}</td> + <td>{{Spec2('ES6')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-object.issealed', 'Object.isSealed')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容">浏览器兼容</h2> + +<div class="hidden"> +<p>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.</p> +</div> + +<p>{{Compat("javascript.builtins.Object.isSealed")}}</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Object.seal()")}}</li> + <li>{{jsxref("Object.preventExtensions()")}}</li> + <li>{{jsxref("Object.isExtensible()")}}</li> + <li>{{jsxref("Object.freeze()")}}</li> + <li>{{jsxref("Object.isFrozen()")}}</li> +</ul> |