TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

JavaScript vs. Python class fields handling

1 点作者 cha42超过 1 年前
I step upon a weirdness exploring oriented object programming in JS.<p>In Python, I often use the following constructions:<p>https:&#x2F;&#x2F;pastebin.com&#x2F;pmnxg0Ji<p>Translating in js we got:<p>https:&#x2F;&#x2F;pastebin.com&#x2F;WpfDnVFX<p>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.<p>The &quot;correct&quot; way to translate the Python code seems then to wrap the fields into a getter which gives actually the following:<p>https:&#x2F;&#x2F;pastebin.com&#x2F;8ZYT6vDe<p>I wonder why JS is implementing such a semantic for fields. It sounds hacky to and feels like field shouldn&#x27;t be mixed with inheritance at all but maybe I have overlooked something?

暂无评论

暂无评论