Aufrufende Variable im Objekt ermitteln
bearbeitet von Rolf BHallo klawischnigg,
okay, das wird klarer.
Jürgen war schneller, die this/that Technik zur Bildung einer Closure funktioniert natürlich auch.
Eine andere Möglichkeit ist Function.prototype.bind. Der bildet die Closure intern:
~~~js
function PopupWindow() {
// do magic
let btnClose = // more magic to create the button
btnClose.addEventListener("click", this.closeWindow.bind(this));
// continue with magic
}
PopupWindow.prototype.closeWindow = function() {
// close the window
}
~~~
`this.closeWindows.bind(this)` erzeugt eine neue Funktion, deren this auf das in dem Moment übergebene this festgenagelt ist.
_Rolf_
--
sumpsi - posui - obstruxi
Aufrufende Variable im Objekt ermitteln
bearbeitet von Rolf BHallo klawischnigg,
okay, das wird klarer. Es klingt so, als bräuchtest Du `bind`.
~~~js
function PopupWindow() {
// do magic
let btnClose = // more magic to create the button
btnClose.addEventListener("click", this.closeWindow.bind(this));
// continue with magic
}
PopupWindow.prototype.closeWindow = function() {
// close the window
}
~~~
this.closeWindows.bind(this) erzeugt eine neue Funktion, deren this auf das in dem Moment übergebene this festgenagelt ist.
_Rolf_
--
sumpsi - posui - obstruxi