I'm looking for a convenient way of maintaining the original context in which a js file operates in at page load.
Technically, a function is called in the js which triggers the construction of someClass
someClass
contains a series of methods which are meant to be part of an API and looks something like:
class someClass{
procGenFunction(){
/* code that procedurally generates a an element <=> " BTN " */
BTN.on('click', fn_called_on_click);
}
fn_called_on_click(){
this.doWhatever(); <--- I need the original context of "THIS" at this point.
}
}
Because JavaScript, the this
keyword references different objects based on where it's called from.
I want to conserve the original value of this
whenever calling any method which is part of the API.
The original value of this
is, as far as I'm concerned, #document
.
TL;DR: I want to always use this
in its original form regardless of where the API calls any function which references that keyword.
It should be worth mentioning that I "absolutely have to" implement this in the most dev-friendly way possible, a.k.a. if possible, not pass this
as an argument to that function every time I want to call it.