Live data from Hacker News

Show HN: Classes for javascript that you'd actually use

github.com

11–20 of 25 posts

Re: Show HN: Classes for javascript that you'd actually use

#11
Pretty 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.

Re: Show HN: Classes for javascript that you'd actually use

#13

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

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

#14

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

Please, please realise most of us are not going to be able to use Object in ES5 till IE8 dies.

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

#15

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

Please, please realise most of us are not going to be able to use Object in ES5 till IE8 dies.

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

#16
post #14

Earlier quoted context omitted.

Please, please realise most of us are not going to be able to use Object in ES5 till IE8 dies.

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.

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

Re: Show HN: Classes for javascript that you'd actually use

#17
Pjs 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 shared object (the prototype).

Re: Show HN: Classes for javascript that you'd actually use

#19
post #17

Pjs 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

#20

Pretty 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.
Post reply on HN