pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Symbol/hasInstance

per.mozilla.org/favicon.ico" />

Dieser Inhalt wurde automatisch aus dem Englischen übersetzt, und kann Fehler enthalten. Erfahre mehr über dieses Experiment.

View in English Always switch to English

Symbol.hasInstance

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2017.

Die Symbol.hasInstance statische Dateneigenschaft repräsentiert das wohlbekannte Symbol Symbol.hasInstance. Der instanceof Operator sucht dieses Symbol auf seinem rechten Operanden, um die Methode zu ermitteln, die verwendet wird, um festzustellen, ob das Konstruktorobjekt ein Objekt als seine Instanz erkennt.

Probieren Sie es aus

class Array1 {
  static [Symbol.hasInstance](instance) {
    return Array.isArray(instance);
  }
}

console.log([] instanceof Array1);
// Expected output: true

Wert

Das wohlbekannte Symbol Symbol.hasInstance.

Eigenschaften von Symbol.hasInstance
Schreibbarnein
Aufzählbarnein
Konfigurierbarnein

Beschreibung

Der instanceof Operator verwendet den folgenden Algorithmus, um den Rückgabewert von object instanceof constructor zu berechnen:

  1. Falls constructor eine [Symbol.hasInstance]() Methode hat, wird diese mit object als erstem Argument aufgerufen und das Ergebnis zurückgegeben, zu einem Boolean umgewandelt. Ein TypeError wird ausgelöst, wenn constructor kein Objekt ist oder wenn constructor[Symbol.hasInstance] weder null, undefined noch eine Funktion ist.
  2. Andernfalls, wenn constructor keine [Symbol.hasInstance]() Methode hat (constructor[Symbol.hasInstance] ist null oder undefined), wird das Ergebnis unter Verwendung des gleichen Algorithmus wie bei Function.prototype[Symbol.hasInstance]() bestimmt. Ein TypeError wird ausgelöst, wenn constructor keine Funktion ist.

Da alle Funktionen standardmäßig von Function.prototype erben, legt in den meisten Fällen die Methode Function.prototype[Symbol.hasInstance]() das Verhalten von instanceof fest, wenn die rechte Seite eine Funktion ist.

Beispiele

Benutzerdefiniertes instanceof Verhalten

Sie könnten Ihr benutzerdefiniertes instanceof Verhalten wie folgt implementieren:

js
class MyArray {
  static [Symbol.hasInstance](instance) {
    return Array.isArray(instance);
  }
}
console.log([] instanceof MyArray); // true
js
function MyArray() {}
Object.defineProperty(MyArray, Symbol.hasInstance, {
  value(instance) {
    return Array.isArray(instance);
  },
});
console.log([] instanceof MyArray); // true

Überprüfen der Instanz eines Objekts

Genauso wie Sie überprüfen können, ob ein Objekt eine Instanz einer Klasse mit dem Schlüsselwort instanceof ist, können wir auch Symbol.hasInstance für solche Überprüfungen verwenden.

js
class Animal {
  constructor() {}
}

const cat = new Animal();

console.log(Animal[Symbol.hasInstance](cat)); // true

Spezifikationen

Specification
ECMAScript® 2026 Language Specification
# sec-symbol.hasinstance

Browser-Kompatibilität

Siehe auch

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy