Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1133885 of Symbol.hasInstance

  • Slug da revisão: Web/JavaScript/Reference/Global_Objects/Symbol/hasInstance
  • Título da revisão: Symbol.hasInstance
  • ID da revisão: 1133885
  • Criado:
  • Autor: GoToLoop
  • É a revisão atual? Não
  • Comentário
Etiquetas: 

Conteúdo da revisão

{{JSRef}}

O symbol bem-conhecido Symbol.hasInstance é usado para determinar se um objecto constructor reconhece um objecto como sua instância. O comportamento do operador {{jsxref("Operators/instanceof", "instanceof")}} pode ser customizado por este symbol.

{{js_property_attributes(0,0,0)}}

Exemplos

Tu podes implementar o comportamento customizado do seu instanceof deste jeito; por exemplo:

class MyArray {  
  static [Symbol.hasInstance](instance) {
    return Array.isArray(instance);
  }
}
console.log([] instanceof MyArray); // true

Especificações

Specification Status Comment
{{SpecName('ES6', '#sec-symbol.hasinstance', 'Symbol.hasInstance')}} {{Spec2('ES6')}} Initial definition.
{{SpecName('ESDraft', '#sec-symbol.hasinstance', 'Symbol.hasInstance')}} {{Spec2('ESDraft')}}  

Compatibilidade com os navegadores

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome(51)}} {{ CompatGeckoDesktop(50) }} {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatUnknown}} {{ CompatGeckoMobile(50) }} {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}}

Veja também

  • {{jsxref("Operators/instanceof", "instanceof")}}

Fonte da revisão

<div>
<p>{{JSRef}}</p>

<p>O symbol&nbsp;bem-conhecido&nbsp;<strong><code>Symbol.hasInstance</code></strong>&nbsp;é usado para&nbsp;determinar se um objecto&nbsp;constructor&nbsp;reconhece um objecto como&nbsp;sua&nbsp;instância. O comportamento do operador {{jsxref("Operators/instanceof", "instanceof")}}&nbsp;pode ser customizado por este symbol.</p>

<p>{{js_property_attributes(0,0,0)}}</p>

<h2 id="Exemplos">Exemplos</h2>

<p>Tu podes implementar o&nbsp;comportamento customizado do seu&nbsp;<code>instanceof</code>&nbsp;deste jeito; por exemplo:</p>

<pre>
class MyArray {  
  static [Symbol.hasInstance](instance) {
    return Array.isArray(instance);
  }
}
console.log([] instanceof MyArray); // true</pre>

<h2 id="Especificações">Especificações</h2>

<table>
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</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>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Compatibilidade_com_os_navegadores">Compatibilidade com os navegadores</h2>

<p>{{CompatibilityTable}}</p>

<table>
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome(51)}}</td>
   <td>{{ CompatGeckoDesktop(50) }}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>

<table>
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{ CompatGeckoMobile(50) }}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Veja_também">Veja também</h2>

<ul>
 <li>{{jsxref("Operators/instanceof", "instanceof")}}</li>
</ul>
</div>
Reverter para esta revisão