A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Error/isError below:

Error.isError() - JavaScript | MDN

Error.isError()

Limited availability

Die statische Methode Error.isError() bestimmt, ob der übergebene Wert ein Error ist.

Syntax Parameter
value

Der zu überprüfende Wert.

Rückgabewert

true, wenn value ein Error ist; andernfalls false.

Beschreibung

Error.isError() überprüft, ob der übergebene Wert ein Error ist. Dies erfolgt durch einen gebrandeten Check für ein privates Feld, das durch den Error()-Konstruktor initialisiert wurde. Dies ist derselbe Mechanismus, der von Array.isArray() verwendet wird, welcher wiederum dem Mechanismus ähnlich ist, der vom in-Operator verwendet wird.

Es ist eine robustere Alternative zu instanceof Error, da es falsche Positiv- und Negativmeldungen vermeidet:

Error.isError() gibt true für DOMException-Instanzen zurück. Dies liegt daran, dass DOMException, obwohl es nicht als echte Unterklasse von Error spezifiziert ist (der Error-Konstruktor ist nicht der Prototyp des DOMException-Konstruktors), für alle gebrandeten Prüfzwecke immer noch wie Error funktioniert.

Beispiele Verwendung von Error.isError()
// all following calls return true
Error.isError(new Error());
Error.isError(new TypeError());
Error.isError(new DOMException());
try {
  1 + 1n;
} catch (e) {
  console.log(Error.isError(e)); // The operation threw a TypeError, so this returns true
}

// all following calls return false
Error.isError();
Error.isError({});
Error.isError(null);
Error.isError(undefined);
Error.isError(17);
Error.isError("Error");
Error.isError(true);
Error.isError(false);
// This is not an error, because the object does not have the private field
// initialized by the Error constructor
Error.isError({ __proto__: Error.prototype });
instanceof vs. Error.isError()

Wenn eine Error-Instanz überprüft wird, ist Error.isError() gegenüber instanceof vorzuziehen, da es über Realms hinweg funktioniert.

const iframe = document.createElement("iframe");
document.body.appendChild(iframe);
const xError = window.frames[window.frames.length - 1].Error;
const error = new xError();

// Correctly checking for Error
Error.isError(error); // true
// The prototype of error is xError.prototype, which is a
// different object from Error.prototype
error instanceof Error; // false
Normalisierung gefangener Fehler

Sie können Error.isError() verwenden, um zu erkennen, ob der gefangene Wert ein Fehler ist und ihn zu einem Fehlerobjekt zu normalisieren.

try {
  throw "Oops; this is not an Error object";
} catch (e) {
  if (!Error.isError(e)) {
    e = new Error(e);
  }
  console.error(e.message);
}
Spezifikationen Browser-Kompatibilität Siehe auch

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4