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.
[ Parent
| on HN ]
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