diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
commit | 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (patch) | |
tree | a9ef8ac1e1b8fe4207b6d64d3841bfb8990b6fd0 /files/uk/web/javascript/reference/global_objects/symbol/hasinstance | |
parent | 074785cea106179cb3305637055ab0a009ca74f2 (diff) | |
download | translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.gz translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.bz2 translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.zip |
initial commit
Diffstat (limited to 'files/uk/web/javascript/reference/global_objects/symbol/hasinstance')
-rw-r--r-- | files/uk/web/javascript/reference/global_objects/symbol/hasinstance/index.html | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/files/uk/web/javascript/reference/global_objects/symbol/hasinstance/index.html b/files/uk/web/javascript/reference/global_objects/symbol/hasinstance/index.html new file mode 100644 index 0000000000..78a1246aa5 --- /dev/null +++ b/files/uk/web/javascript/reference/global_objects/symbol/hasinstance/index.html @@ -0,0 +1,81 @@ +--- +title: Symbol.hasInstance +slug: Web/JavaScript/Reference/Global_Objects/Symbol/hasInstance +tags: + - ECMAScript 2015 + - JavaScript + - 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="Приклади">Приклади</h2> + +<h3 id="Змінена_поведінка_instanceof">Змінена поведінка instanceof</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="Перевіряємо_екземпляр_обєкта">Перевіряємо екземпляр об'єкта</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="Специфікації">Специфікації</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="Сумісність_з_веб-переглядачами">Сумісність з веб-переглядачами</h2> + + + +<p>{{Compat("javascript.builtins.Symbol.hasInstance")}}</p> + +<h2 id="Див._також">Див. також</h2> + +<ul> + <li>{{jsxref("Operators/instanceof", "instanceof")}}</li> +</ul> |