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 : Uppercasing a String
4+ chapter : Strings
5+ ---
6+ ## Problem
7+
8+ You want to uppercase a string.
9+
10+ ## Solution
11+
12+ Use JavaScript's String toUpperCase() method:
13+
14+ {% highlight coffeescript %}
15+ "one two three".toUpperCase()
16+ # => 'ONE TWO THREE'
17+ {% endhighlight %}
18+
19+ ## Discussion
20+
21+ ` toUpperCase() ` 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::upcase = -> @toUpperCase ()
29+ "one two three".upcase()
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.upcase = function() {
42+ return this.toUpperCase();
43+ };
44+ "one two three".upcase();
45+ {% endhighlight %}
You can’t perform that action at this time.
0 commit comments