Please, please read http://killdream.github.com/blog/2011/10/understanding-javas... Prototypical inheritance as of ES5 is really not that hard. Here is the same example rewritten with prototypes: https://gist.github.com/1558929
Show HN: Classes for javascript that you'd actually use
21–25 of 25 posts
Re: Show HN: Classes for javascript that you'd actually use
#22Pretty nice. Ends up being similar in structure to classes in coffeescript. 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.
Yeah, I'll admit that's a bit confusing. I did want to provide a way do something like Object.create in environments where it's not available. This way you can do `MySubclass.prototype = new MyClass` and not have to worry about the initializer.
Doing away with `new` in this way also has the added advantage of allowing people to create objects with varargs, which is impossible otherwise.
Re: Show HN: Classes for javascript that you'd actually use
#23Pjs may work for a small number of objects, but if you're dealing with a large number of intances from a single class Pjs is going to use much more memory than needed. That's because the recommended method P(function () { this.method = fn; }) is declaring all methods as instances properties instead of using the prototype, so all methods are actually new functions in new own properties instead of properties of a share…
I should have mentioned it in the README, but if you read carefully, the function you pass to P() is only called once, and you're adding functions to the prototype. So it's actually pretty memory-efficient.
Re: Show HN: Classes for javascript that you'd actually use
#24Earlier quoted context omitted.
Yeah, I'll admit that's a bit confusing. I did want to provide a way do something like Object.create in environments where it's not available. This way you can do `MySubclass.prototype = new MyClass` and not have to worry about the initializer.
(apologies for the double post) 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?
Re: Show HN: Classes for javascript that you'd actually use
#25Earlier quoted context omitted.
(apologies for the double post) 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 :)