File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ layout : recipe
3
+ title : Lowercasing a String
4
+ chapter : Strings
5
+ ---
6
+ ## Problem
7
+
8
+ You want to lowercase a string.
9
+
10
+ ## Solution
11
+
12
+ Use JavaScript's String toLowerCase() method:
13
+
14
+ {% highlight coffeescript %}
15
+ "ONE TWO THREE".toLowerCase()
16
+ # => 'one two three'
17
+ {% endhighlight %}
18
+
19
+ ## Discussion
20
+
21
+ ` toLowerCase() ` is a standard JavaScript method. Don't forget the parentheses.
22
+
23
+ ### Syntax Sugar
24
+
25
+ You can add some Ruby-like syntax sugar with the following shortcut:
26
+
27
+ {% highlight coffeescript %}
28
+ String::downcase = -> @toLowerCase ()
29
+ "ONE TWO THREE".downcase()
30
+ # => 'one two three'
31
+ {% endhighlight %}
32
+
33
+ The snippet above demonstrates a few features of CoffeeScript:
34
+
35
+ * The double-colon ` :: ` is shorthand for saying ` .prototype. `
36
+ * The "at" sign ` @ ` is shorthand for saying ` this. `
37
+
38
+ The code above compiles in to the following JavaScript:
39
+
40
+ {% highlight javascript %}
41
+ String.prototype.downcase = function() {
42
+ return this.toLowerCase();
43
+ };
44
+ "ONE TWO THREE".downcase();
45
+ {% endhighlight %}
You can’t perform that action at this time.
0 commit comments