Discussion

JavaScript vs. Python class fields handling

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?

1 points cha42 2 years ago 0 comments

No comments yet

Be the first to share your thoughts.

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton