Created
September 3, 2011 14:22
-
-
Save zolzaya/1191257 to your computer and use it in GitHub Desktop.
CoffeeScript overview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # CoffeeScript-н бичиглэл | |
| # Assignment: | |
| number = 42 | |
| opposite = true | |
| # JavaScript-рүү хөрвүүлсэн байдал | |
| var number = 42; | |
| var opposite = true; | |
| # CoffeeScript-н бичиглэл | |
| # Conditions: | |
| number = -42 if opposite | |
| # JavaScript-рүү хөрвүүлсэн байдал | |
| if (opposite) number = -42; | |
| # CoffeeScript-н бичиглэл | |
| # Functions: | |
| square = (x) -> x*x | |
| # JavaScript-рүү хөрвүүлсэн байдал | |
| var square = function(x) { | |
| return x * x; | |
| }; | |
| # CoffeeScript-н бичиглэл | |
| # Arrays: | |
| list = [1, 2, 3, 4, 5] | |
| # JavaScript-рүү хөрвүүлсэн байдал | |
| var list = [1, 2, 3, 4, 5]; | |
| # CoffeeScript-н бичиглэл | |
| # Objects: | |
| math = | |
| root: Math.sqrt | |
| square: square | |
| cube: (x) -> x * square x | |
| # JavaScript-рүү хөрвүүлсэн байдал | |
| var math = { | |
| root: Math.sqrt, | |
| square: square, | |
| cube: function(x) { | |
| return x * square(x); | |
| } | |
| }; | |
| # CoffeeScript-н бичиглэл | |
| # Splats | |
| race = (winner, runners...) -> | |
| print winner, runners | |
| # JavaScript-рүү хөрвүүлсэн байдал | |
| var __slice = Array.prototype.slice; | |
| var race = function() { | |
| var runners, winner; | |
| winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : []; | |
| return print(winner, runners); | |
| }; | |
| # CoffeeScript-н бичиглэл | |
| # Existence: | |
| alert "I knew it!" if elvis? | |
| # JavaScript-рүү хөрвүүлсэн байдал | |
| if (typeof elvis !== "undefined" && elvis !== null) alert("I knew it!"); | |
| # CoffeeScript-н бичиглэл | |
| # Array comprehensions: | |
| cubes = (math.cube num for num in list) | |
| # JavaScript-рүү хөрвүүлсэн байдал | |
| var cubes = (function() { | |
| var _i, _len, _results; | |
| _results = []; | |
| for (_i = 0, _len = list.length; _i < _len; _i++) { | |
| num = list[_i]; | |
| _results.push(math.cube(num)); | |
| } | |
| return _results; | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment