I want to fire a function directly, when a new instance of my class is initiated. And it should have access to this. I ve read this question and both of my methods firing, but none have access to the this. (What I realy want accessing are the options ;-) )
I've tried a) in es6 constructor and b) with Prototype Syntax so far. Both fireing, but this is undefined in both cases.
class TypewriterSlider {
constructor(sliderElem, userOptions) {
this.sliderElem = sliderElem;
this.options = {
...userOptions,
split: true,
}
this.myInitLettering = function () {
console.log('a:', this) //->a, undefined
}()
}
}
TypewriterSlider.prototype.initLettering = (function () {
console.log('b: ', this) // ->b, undefined
})()
const tws = new TypewriterSlider(elem, {})
I hope someone can help me out. Is it possible to fire a method with access to this, without explicit call? Or is there a better way to achive the desired behavior?
Sorry for my English and many thanks in advance.