Settings

Theme

JavaScript vs. Python class fields handling

1 points by cha42 2 years ago · 0 comments · 1 min read


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?

No comments yet.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection