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?
Going back to Smalltalk, it seems like original OO was prototypal. I wonder what made them change this.<p>Here's the article I was just looking at:<p><a href="http://onsmalltalk.com/objects-classes-and-constructors-smalltalk-style" rel="nofollow">http://onsmalltalk.com/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'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.</code></pre>
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://outof.me/javascript-prototypal-inheritance-for-classical-developers/" rel="nofollow">http://outof.me/javascript-prototypal-inheritance-for-classi...</a><p>* <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript#Inheritance" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Intr...</a><p>* <a href="http://stackoverflow.com/questions/8453887/why-is-it-necessary-to-set-the-prototype-constructor" rel="nofollow">http://stackoverflow.com/questions/8453887/why-is-it-necessa...</a>
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.