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/symbol/hasinstance | |
| 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/symbol/hasinstance')
| -rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/symbol/hasinstance/index.html | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/symbol/hasinstance/index.html b/files/zh-cn/web/javascript/reference/global_objects/symbol/hasinstance/index.html new file mode 100644 index 0000000000..ad28084f8e --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/symbol/hasinstance/index.html @@ -0,0 +1,59 @@ +--- +title: Symbol.hasInstance +slug: Web/JavaScript/Reference/Global_Objects/Symbol/hasInstance +translation_of: Web/JavaScript/Reference/Global_Objects/Symbol/hasInstance +--- +<p>{{JSRef}}</p> + +<p><strong><code>Symbol.hasInstance</code></strong>用于判断某对象是否为某构造器的实例。因此你可以用它自定义 {{jsxref("Operators/instanceof", "instanceof")}} 操作符在某个类上的行为。</p> + +<div> +<div>{{EmbedInteractiveExample("pages/js/symbol-hasinstance.html")}}</div> + +<div 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 href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</div> +</div> + +<div>{{js_property_attributes(0,0,0)}}</div> + +<h2 id="示例">示例</h2> + +<p><font face="Open Sans, Arial, sans-serif">你可实现一个自定义的</font><code>instanceof</code> 行为,<font face="Open Sans, Arial, sans-serif">例如:</font></p> + +<pre class="brush: js notranslate">class MyArray { + static [Symbol.hasInstance](instance) { + return Array.isArray(instance); + } +} +console.log([] instanceof MyArray); // true</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">文档</th> + <th scope="col">状态</th> + <th scope="col">备注</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-symbol.hasinstance', 'Symbol.hasInstance')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-symbol.hasinstance', 'Symbol.hasInstance')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("javascript.builtins.Symbol.hasInstance")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{jsxref("Operators/instanceof", "instanceof")}}</li> +</ul> |
