
Ask HN: What's the point of a constructor function anyways? - ovatsug25
While trying out a project in Javascript, I decided to finally learn a bit about the &quot;prototype&quot;. I&#x27;m at the crossroads where I want to decide whether I want to use &quot;pure&quot; prototypal inheritance as with &quot;Object.create&quot; or whether I want to go with &quot;new Class(params...)&quot;. As I investigate and weight between my choices, I realize that &quot;When Object.create is used, it is like having only the prototype objects, and empty constructors.&quot; So what is the point of constructors anyways? Picking between Object.create and &quot;new&quot; is based on how important are constructors for getting things done. Can you help me brainstorm through this?
======
ovatsug25
Going back to Smalltalk, it seems like original OO was prototypal. I wonder
what made them change this.

Here's the article I was just looking at:

[http://onsmalltalk.com/objects-classes-and-constructors-
smal...](http://onsmalltalk.com/objects-classes-and-constructors-smalltalk-
style)

Choice quotes

    
    
         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's write a constructor for #Person, we'll put it on the "Person class", or the Person's class side as it'd normally be called.
    
    
          Some Smalltalk'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>>firstName: aFirstName lastName: aLastName
        ^(self new)
            initializeFirstName: aFirstName lastName: aLastName;
            yourself.
    
          Person>>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.

~~~
ovatsug25
Another great article:

[http://programmers.stackexchange.com/questions/224033/can-
we...](http://programmers.stackexchange.com/questions/224033/can-we-live-
without-constructors)

Wish I found this one from the very beginning!

------
jtfairbank
I ended up using constructors to create instances of an object, and
Object.create to enable inheritance when defining the object.

    
    
        function Person(name, birthday, ...) {
          ...
        }
    
        Person.prototype = Object.create(Animal.prototype);
    
    

See these for more:

* [http://outof.me/javascript-prototypal-inheritance-for-classi...](http://outof.me/javascript-prototypal-inheritance-for-classical-developers/)

* [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Intr...](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript#Inheritance)

* [http://stackoverflow.com/questions/8453887/why-is-it-necessa...](http://stackoverflow.com/questions/8453887/why-is-it-necessary-to-set-the-prototype-constructor)

------
wturner
The point of constructors in JavaScript was to mirror the "look" of Java.
Brendan Eich has mentioned this. You don't technically need them and can use
functions to return objects.

