News for hackers. Stuff that matters.
Re:Show HN: Classes for javascript that you'd actually use by jarek-foksa on Wednesday January 04, 2012 @01:56AM (#3423122)
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

Re:Show HN: Classes for javascript that you'd actually use by jayferd on Wednesday January 04, 2012 @01:38PM (#3425264)
This is why Pjs is so small - it's just prototypal inheritance, with the bad parts taken out. You have to know how prototypes work to use Pjs.
Re:Show HN: Classes for javascript that you'd actually use by mattmanser on Wednesday January 04, 2012 @06:57AM (#3423754)
Please, please realise most of us are not going to be able to use Object in ES5 till IE8 dies.
Re:Show HN: Classes for javascript that you'd actually use by bmuon on Wednesday January 04, 2012 @09:12AM (#3424156)
Object.create has two parts: the "create a new object with this other object as its prototype" part and the "define properties" part. The first part can be easily reproduced in ES3 and it's the most useful part. It's really well explained in this article by Douglas Crockford http://javascript.crockford.com/prototypal.html.
Re:Show HN: Classes for javascript that you'd actually use by mixu on Wednesday January 04, 2012 @08:00AM (#3423918)
Well, how about this? I'm fairly certain it's ES 3 compatible.

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.

Re:Show HN: Classes for javascript that you'd actually use by bmuon on Wednesday January 04, 2012 @09:16AM (#3424174)
Foo.prototype = new Bar() has the problem of setting the superclass' instance properties as prototype properties of the subclass. This can be solved pretty easily with something like Object.create (the first part explained here http://news.ycombinator.com/item?id=3424156). Here's a different example in 30 lines: https://gist.github.com/1032636