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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Ask HN: What's the point of a constructor function anyways?

6 点作者 ovatsug25超过 9 年前
While trying out a project in Javascript, I decided to finally learn a bit about the "prototype". I'm at the crossroads where I want to decide whether I want to use "pure" prototypal inheritance as with "Object.create" or whether I want to go with "new Class(params...)". As I investigate and weight between my choices, I realize that "When Object.create is used, it is like having only the prototype objects, and empty constructors." So what is the point of constructors anyways? Picking between Object.create and "new" is based on how important are constructors for getting things done. Can you help me brainstorm through this?

3 条评论

ovatsug25超过 9 年前
Going back to Smalltalk, it seems like original OO was prototypal. I wonder what made them change this.<p>Here&#x27;s the article I was just looking at:<p><a href="http:&#x2F;&#x2F;onsmalltalk.com&#x2F;objects-classes-and-constructors-smalltalk-style" rel="nofollow">http:&#x2F;&#x2F;onsmalltalk.com&#x2F;objects-classes-and-constructors-smal...</a><p>Choice quotes<p><pre><code> But a constructor would really help pretty this up and encapsulate the knowledge about what fields are required for construction rather that forcing the client to individually initialize the object and hoping he does it correctly. So let&#x27;s write a constructor for #Person, we&#x27;ll put it on the &quot;Person class&quot;, or the Person&#x27;s class side as it&#x27;d normally be called. Some Smalltalk&#x27;ers prefer to write their constructors in a different style, one that enforces encapsulation a bit more, by not allowing public setters for all the instance variables, like so... Person class&gt;&gt;firstName: aFirstName lastName: aLastName ^(self new) initializeFirstName: aFirstName lastName: aLastName; yourself. Person&gt;&gt;initializeFirstName: aFirstName lastName: aLastName firstName := aFirstName. lastName := aLastName. relatives := Set new. Smalltalk is not so much a language, as an extensible notation for creating languages. To rip off a famed martial artist, empty your mind, be formless, shapeless, like Smalltalk. If you put Smalltalk into a financial application, it becomes a custom finance language. You put Smalltalk into a shipping application, and it becomes a custom shipping language. Be Smalltalk my friend.</code></pre>
评论 #11022607 未加载
评论 #11025284 未加载
jtfairbank超过 9 年前
I ended up using constructors to create instances of an object, and Object.create to enable inheritance when defining the object.<p><pre><code> function Person(name, birthday, ...) { ... } Person.prototype = Object.create(Animal.prototype); </code></pre> See these for more:<p>* <a href="http:&#x2F;&#x2F;outof.me&#x2F;javascript-prototypal-inheritance-for-classical-developers&#x2F;" rel="nofollow">http:&#x2F;&#x2F;outof.me&#x2F;javascript-prototypal-inheritance-for-classi...</a><p>* <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Introduction_to_Object-Oriented_JavaScript#Inheritance" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Intr...</a><p>* <a href="http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;8453887&#x2F;why-is-it-necessary-to-set-the-prototype-constructor" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;8453887&#x2F;why-is-it-necessa...</a>
wturner超过 9 年前
The point of constructors in JavaScript was to mirror the &quot;look&quot; of Java. Brendan Eich has mentioned this. You don&#x27;t technically need them and can use functions to return objects.