I am a pythonista who is learning js.
# python
class A():
def __init__(self, a):
self.a = a
def main(self):
print(self.a)
a = A(1)
b = a.main
a.main() # 1
b() # 1
// js
class A {
constructor(a) {
this.a = a;
}
main() {
console.log(this.a)
}
}
a = new A(1);
b = a.main
console.log(Object.is(b, a.main)); // true
a.main(); // 1
b(); // TypeError: Cannot read property 'a' of undefined # why is not 1?
I know b need bind(a) like:
b = b.bind(a)
But my question is what is different between python and js? Why python can do it but js can't. I guess the reason is about underlying implementation mechanism of python and js is different, but I don't know. Thanks in advance.