small medium large xlarge

Back to: All Forums  CoffeeScript
11 May 2011, 09:54
Hans Christian von Stockhausen (3 posts)

Great book. A joy to read and easy to follow so far. However I’d like chapter 4.2 ‘The Power of Prototypes’ and 4.3 on ‘Classes: Functions with Prototypes’ to give a few more examples.

One point I don’t quite understand is on p 58-59 (document coffeescript_b2_0.pdf):

Gift = (@name) -> 
  @day = Gift.count 

Each time the Gift constructor runs, it does four things: Assigns the given name to @name (using the argument shorthand), increments the count property on the Gift prototype, copies that value to @day, and runs the @announce function inherited from the prototype. The important thing to notice here is that all of the functions on the new object run in the context of the object.

Why is count a property of the prototype? Isn’t it a property of the constructor?

Regards, HC

11 May 2011, 16:52
Trevor Burnham (53 posts)

Thanks for spotting this. You’re right, that’s a typo. In an earlier version, the code read

Gift = (@name) -> 
  @day = Gift.count 

Gift::Count = 0

but then someone pointed out that this is unnecessarily confusing, since in practice you’d never put a property like @count@ on the prototype. (I was trying to make a point about how confusing that can be, which I moved to an exercise instead.) So, the paragraph should read

increments the count property on the Gift constructor

This will be corrected in the next beta release.

You must be logged in to comment