I stopped using constructors (and prototypes) a couple of years ago and it's improved my relationship with JS tremendously.
A couple more benefits:
1. You never need to see "this" again. You can refer to a method as object.method, and when you call that reference, you're calling the method on that object, just like in, say, Python. No "apply" madness needed.
2. If you decide your object might take some time to construct, you can change your "makeObject" function to be asynchronous (using promises or Node-style callbacks). With a constructor you just can't do this.
I've come to think of the constructor/prototype system as one of those bits that was bolted on to the rather clean "base" language of JS to meet Netscape's demand for a "java-like" language. You can really do without it.
It sounds like you're explicitly adding function properties to every object. This is slow and wasteful or memory. In Python, method lookup scans the __class__ of the object to find the method function. Python also automatically binds the object instance to the first (self) parameter.
What you're describing does not bear much resemblance to Python.
This is true, but I believe that in modern JS implementations, the performance difference isn't as large as one might expect.
The similarity to Python is limited to syntax — I just wanted to raise the point that in both Python and prototype-free JS, higherOrderFunction(object.method) does what it looks like it will do.
I think the comment author you responded to, meant that the developers behind JavaScript was "forced" to add the "new" keyword simply to make JavaScript more Java-like. Not that JavaScript actually is like Java.
Comments
I stopped using constructors (and prototypes) a couple of years ago and it's improved my relationship with JS tremendously.
A couple more benefits:
1. You never need to see "this" again. You can refer to a method as object.method, and when you call that reference, you're calling the method on that object, just like in, say, Python. No "apply" madness needed.
2. If you decide your object might take some time to construct, you can change your "makeObject" function to be asynchronous (using promises or Node-style callbacks). With a constructor you just can't do this.
I've come to think of the constructor/prototype system as one of those bits that was bolted on to the rather clean "base" language of JS to meet Netscape's demand for a "java-like" language. You can really do without it.
It sounds like you're explicitly adding function properties to every object. This is slow and wasteful or memory. In Python, method lookup scans the __class__ of the object to find the method function. Python also automatically binds the object instance to the first (self) parameter.
What you're describing does not bear much resemblance to Python.
This is true, but I believe that in modern JS implementations, the performance difference isn't as large as one might expect.
The similarity to Python is limited to syntax — I just wanted to raise the point that in both Python and prototype-free JS, higherOrderFunction(object.method) does what it looks like it will do.
which part of prototype is java-like?
I think the comment author you responded to, meant that the developers behind JavaScript was "forced" to add the "new" keyword simply to make JavaScript more Java-like. Not that JavaScript actually is like Java.
The new keyword was absolutely added to be Java-like however the "prototype system" is nothing like Java.
I don't think Netscape demanded a "java-like" language.
http://en.wikipedia.org/wiki/Talk%3AECMAScript, search for "make it look like Java" and see who wrote the comment.
Well there you go, interesting. The "see who wrote the comment" comment was unnecessary though as it's pretty obvious.