I'm a newbie with Node/Javascript (ES6). See example here below:
class MyClass {
myTest() {
console.log('it works')
}
runMyTest() {
this.myTest()
}
}
if I omit the this. in the this.myTest() line I get the runtime error:
myTest() ^ReferenceError: myTest is not defined
It seems to me to be mandatory to invoke methods declared in the same object (a Class Object in this case) of the caller, need the this.method()
That's correct?
In a similar way, I see that a parent method (of a subclassed object) requires super.parentMethod().
Coming from Ruby/other OO languages that sound weird to me.
Why the reason for mandatory this. / super. in JS?
UPDATE:
small workaround I found (to avoid this.method repetition):
class MyClass {
myTest() {
console.log('it works')
}
runMyTestManyTimes() {
const mytest = this.mytest
myTest()
...
myTest()
...
myTest()
...
}
}

thisis the calling context, whereas omission ofthiswill only lookup the identifier from the scope, and call the function without a calling context if it's in scope, which in this case it's not.@forthis. Does it really let you call methods with no referent?