Skip to content

Commit 80b9fa2

Browse files
David Bradysreid
authored andcommitted
Added objects/extending-classes and and strings/capitalizing-words
1 parent 67b1e8a commit 80b9fa2

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
layout: recipe
3+
title: Extending Classes
4+
chapter: Objects
5+
---
6+
7+
h2. Problem
8+
9+
You want to extend a class to add new functionality or replace old.
10+
11+
h2. Solution
12+
13+
Assign your new function to the prototype of the object or class.
14+
15+
{% highlight coffeescript %}
16+
String.prototype.capitalize = () ->
17+
(this.split(/\s+/).map (word) -> word[0].toUpperCase() + word[1..-1].toLowerCase()).join ' '
18+
19+
"foo bar baz".capitalize()
20+
# => 'Foo Bar Baz'
21+
{% endhighlight %}
22+
23+
h2. Discussion
24+
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.
28+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
layout: recipe
3+
title: Capitalizing Words
4+
chapter: Strings
5+
---
6+
7+
h2. Problem
8+
9+
You want to capitalize the first letter of every word in a string.
10+
11+
h2. Solution
12+
13+
Use the split, map, join pattern: Split the string into words, then use a map to capitalize the first letter and lowercase all other letters of each word before gluing the string back together with join.
14+
15+
{% highlight coffeescript %}
16+
("foo bar baz".split(' ').map (word) -> word[0].toUpperCase() + word[1..-1].toLowerCase()).join ' '
17+
# => 'Foo Bar Baz'
18+
{% endhighlight %}
19+
20+
Or do the same thing using a list comprehension:
21+
22+
{% highlight coffeescript %}
23+
(word[0].toUpperCase() + word[1..-1].toLowerCase() for word in "foo bar baz".split /\s+/).join ' '
24+
# => 'Foo Bar Baz'
25+
{% endhighlight %}
26+
27+
h2. Discussion
28+
29+
Split, map, join is a common scripting pattern dating back to perl. This function may benefit from being placed directly onto the String class by <a href="/chapters/objects/extending-classes">Extending Classes</a>.
30+
31+
Be aware that two wrinkles can appear in the split, map, join pattern. The first is that the split text works best when it is constant. If the source string has multiple spaces in it, the split will need to take this into account to prevent getting extra, empty words. One way to do this is with a regular expression to split on runs of whitespace instead of a single space:
32+
33+
{% highlight coffeescript %}
34+
("foo bar baz".split(/\s+/).map (word) -> word[0].toUpperCase() + word[1..-1].toLowerCase()).join ' '
35+
# => 'Foo Bar Baz'
36+
{% endhighlight %}
37+
38+
...but this leads us to the second wrinkle: notice that the runs of whitespace are now compressed down to a single character by the join.
39+
40+
Quite often one or both of these wrinkles is acceptable, however, so the split, map, join pattern can be a powerful tool.
41+

0 commit comments

Comments
 (0)