Window: self-Eigenschaft

Baseline Widely available

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

Die schreibgeschützte Window.self-Eigenschaft gibt das Fenster selbst zurück, als ein WindowProxy. Sie kann mit Punktnotation auf einem window-Objekt verwendet werden (d. h. window.self) oder eigenständig (self). Der Vorteil der eigenständigen Notation besteht darin, dass eine ähnliche Notation für Nicht-Fenster-Kontexte existiert, wie z. B. in Web Workers. Durch die Verwendung von self kann man auf den globalen Kontext in einer Art und Weise verweisen, die nicht nur im Fenstermenü-Kontext funktioniert (self wird zu window.self aufgelöst), sondern auch im Worker-Kontext (self wird dann zu WorkerGlobalScope.self).

Wert

Ein WindowProxy-Objekt.

Beispiele

Verwendungen von window.self wie die folgenden könnten ebenso gut durch window ersetzt werden.

js
if (window.parent.frames[0] !== window.self) {
  // this window is not the first frame in the list
}

Außerdem, wenn in dem aktiven Dokument eines Browsing-Kontextes ausgeführt, ist window ein Verweis auf das aktuelle globale Objekt, und somit sind alle folgenden Äquivalente:

js
const w1 = window;
const w2 = self;
const w3 = window.window;
const w4 = window.self;
// w1, w2, w3, w4 all strictly equal, but only w2 will function in workers

Spezifikationen

Specification
HTML
# dom-self-dev

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
self

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Siehe auch