diff options
Diffstat (limited to 'files/ja/web/javascript/reference/global_objects/symbol/hasinstance/index.html')
| -rw-r--r-- | files/ja/web/javascript/reference/global_objects/symbol/hasinstance/index.html | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/files/ja/web/javascript/reference/global_objects/symbol/hasinstance/index.html b/files/ja/web/javascript/reference/global_objects/symbol/hasinstance/index.html new file mode 100644 index 0000000000..878bb5ea98 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/symbol/hasinstance/index.html @@ -0,0 +1,80 @@ +--- +title: Symbol.hasInstance +slug: Web/JavaScript/Reference/Global_Objects/Symbol/hasInstance +tags: + - ECMAScript 2015 + - JavaScript + - Property + - Reference + - Symbol +translation_of: Web/JavaScript/Reference/Global_Objects/Symbol/hasInstance +--- +<div>{{JSRef}}</div> + +<p><span class="seoSummary"><strong><code>Symbol.hasInstance</code></strong> は、コンストラクターオブジェクトが、そのインスタンスのオブジェクトとして認識されるかどうかを決定するために使用されます。このシンボルで、{{jsxref("Operators/instanceof", "instanceof")}} 演算子の動作をカスタマイズすることができます。</span></p> + +<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>{{js_property_attributes(0,0,0)}}</div> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Custom_instanceof_behavior" name="Custom_instanceof_behavior">独自のインスタンスでの動作</h3> + +<p>たとえば、次のようにして <code>instanceof</code> の独自の動作を実装することができます。</p> + +<pre class="brush: js notranslate">class MyArray { + static [Symbol.hasInstance](instance) { + return Array.isArray(instance) + } +} +console.log([] instanceof MyArray); // true +</pre> + +<pre class="brush: js notranslate">function MyArray() { } +Object.defineProperty(MyArray, Symbol.hasInstance, { + value: function(instance) { return Array.isArray(instance); } +}); +console.log([] instanceof MyArray); // true</pre> + +<h3 id="Checking_the_instance_of_an_object" name="Checking_the_instance_of_an_object">オブジェクトのインスタンスを確認する</h3> + +<p><code>instanceof</code> キーワードを使ってオブジェクトがクラスのインスタンスであるかどうかを確認するのと同じ方法で、<code>Symbol.hasInstance</code> を使って確認することもできます。</p> + +<pre class="brush: js notranslate">class Animal { + constructor() {} +} + +const cat = new Animal(); + +console.log(Animal[Symbol.hasInstance](cat)); // true +</pre> + +<h2 id="Specifications" name="Specifications">仕様</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">仕様書</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-symbol.hasinstance', 'Symbol.hasInstance')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> + +<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、<a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> + +<p>{{Compat("javascript.builtins.Symbol.hasInstance")}}</p> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>{{jsxref("Operators/instanceof", "instanceof")}}</li> +</ul> |
