Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
JavaScript vs. Python class fields handling
1 point by cha42 on Feb 20, 2024 | hide | past | favorite
I step upon a weirdness exploring oriented object programming in JS.

In Python, I often use the following constructions:

https://pastebin.com/pmnxg0Ji

Translating in js we got:

https://pastebin.com/WpfDnVFX

Reading the doc, I understood that JS is initializing fields after the super is called. Hence, fields and methods behave differently with respect to inheritance.

The "correct" way to translate the Python code seems then to wrap the fields into a getter which gives actually the following:

https://pastebin.com/8ZYT6vDe

I wonder why JS is implementing such a semantic for fields. It sounds hacky to and feels like field shouldn't be mixed with inheritance at all but maybe I have overlooked something?



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: