Show HN: Classes for javascript that you'd actually use
github.com
github.com
Prototypical inheritance as of ES5 is really not that hard. Here is the same example rewritten with prototypes: https://gist.github.com/1558929
https://gist.github.com/1559933
The original was 28 lines, this is 36 (mostly because I like white space).
I haven't ever really tried to emulate classes in JS, but this is probably how I would do it. The major difference is that this is written plain old Javascript rather than some special syntax - easier to understand.
What benefits do developers get from using a 3rd-party library to create classes in a prototype-based OOP language? Why can't developers just learn that there is more than one style of OOP, and go with the flow?
The very last lines worry me a bit though:
// instantiate objects by calling the class
MyClass() // => init!
// allocate blank objects with `new`
new MyClass // nothing logged
Having `MyClass()` and `new MyClass` behave differently is totally unwarranted and will cause lots of confusion.Doing away with `new` in this way also has the added advantage of allowing people to create objects with varargs, which is impossible otherwise.
new MyClass('bacon', 'eggs')
is perfectly possible, or am I misunderstanding your sentence?How about exposing creation via `P.create`, `P.inherit` or something like it?
EDIT: also, a class method would hurt your script's minifier performance. ...and at that point you may as well just call the function anyways :)
(Note: I was only introduced to MooTools today, so I have limited knowledge about it.)
It seems every couple weeks javascript class library named X is released, avoiding issue Y, and implementing fuctionality Z.
Using `prototype` isn't terribly far off from the class systems people keep creating, so I can pretty much assume a single class library is going to reach deep adoption anytime soon.