Skip to content

Commit 1b56d99

Browse files
author
David Brady
committed
Updated extending-classes to be idiomatic CoffeeScript
1 parent 3c02a9a commit 1b56d99

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

chapters/objects/extending-classes.textile

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ You want to extend a class to add new functionality or replace old.
1010

1111
h2. Solution
1212

13-
Assign your new function to the prototype of the object or class.
13+
Use :: to assign your new function to the prototype of the object or class.
1414

1515
{% highlight coffeescript %}
16-
String.prototype.capitalize = () ->
16+
String::capitalize = () ->
1717
(this.split(/\s+/).map (word) -> word[0].toUpperCase() + word[1..-1].toLowerCase()).join ' '
1818

1919
"foo bar baz".capitalize()
@@ -22,7 +22,5 @@ String.prototype.capitalize = () ->
2222

2323
h2. Discussion
2424

25-
Objects in JavaScript (and thus, in CoffeeScript) have a prototype member that defines what member functions should be available on all objects based on that prototype. Or, if you prefer more object-oriented terminology, functions added to the prototype object are shared among all instances of that class.
26-
27-
If this were a JavaScript Cookbook, we would now have a spirited debate over the fact that you cannot extend a class in JavaScript because it doesn't actually have classes, but instead merely prototype objects. Thank goodness this is a CoffeeScript Cookbook! Because now we don't have to be silly.
25+
Objects in JavaScript (and thus, in CoffeeScript) have a prototype member that defines what member functions should be available on all objects based on that prototype. In CoffeeScript, you can access the prototype directly via the :: operator.
2826

0 commit comments

Comments
 (0)