Live data from Hacker News

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

github.com

21–25 of 25 posts

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

#21

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

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

#22
post #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.

(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.

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

#23
post #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.

Very interesting! I'll re-read the code.

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

#24
post #22
post #20

Earlier 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

#25
post #22

Earlier 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?

right, but say you had an `var array = ['bacon', 'eggs']`. You can call a function with these arguments with `fn.apply(null, array)` (yeah it's ugly but it's how it's done in JS). This doesn't work with the `new` keyword.

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 :)

Post reply on HN