Created
June 3, 2012 03:03
-
-
Save skusunam/2861548 to your computer and use it in GitHub Desktop.
Object literals & Array definitions & String interpolations
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
| array1 = [1, 2, 3] | |
| array2 = [ | |
| 1 | |
| 2 | |
| 3 | |
| ] | |
| array3 = [1,2,3,] | |
| # if a range is not prefixed by anything, Coffeescript expands it out into an array | |
| range = [1..5] | |
| # if the range is specified immediately after a variable, CoffeeScript converts it into a slice() method call | |
| firstTwo = ["one", "two", "three"][0..1] | |
| # you can use ranges with strings too | |
| my = "my string"[0..2] | |
| # check if a value exists inside an array | |
| words = ["rattled", "roudy", "rebbles", "ranks"] | |
| alert "Stop wagging me" if "ranks" in words | |
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
| var array1, array2, array3; | |
| array1 = [1, 2, 3]; | |
| array2 = [1, 2, 3]; | |
| array3 = [1, 2, 3]; | |
| # if a range is not prefixed by anything, Coffeescript expands it out into an array | |
| var range; | |
| range = [1,2,3,4,5]; | |
| # if the range is specified immediately after a variable, CoffeeScript converts it into a slice() method call | |
| var firstTwo; | |
| firstTwo = ["one", "two", "three"].slice(0, 2); | |
| # you can use ranges with strings too | |
| var my; | |
| my = "my string".slice(0, 2); | |
| # check if a value exists inside an array | |
| var words; | |
| var __indexOf = Array.prototype.indexOf || function(item) { | |
| for (var i = 0, l = this.length; i < l; i++) { | |
| if (this[i] === item) return i; | |
| } | |
| return -1; | |
| }; | |
| words = ["rattled", "roudy", "rebbles", "ranks"]; | |
| if (__indexOf.call(words, "ranks") >= 0) { | |
| alert("Stop wagging me"); | |
| } |
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
| object1 = {one : 1, two : 2} | |
| #without braces | |
| object2 = one : 1, two : 2 | |
| #using new lines instead of commas | |
| object3 = | |
| one : 1 | |
| two : 2 | |
| User.create(name : "Hello Object") |
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
| var object1, object2, object3; | |
| object1 = { | |
| one: 1, | |
| two: 2 | |
| }; | |
| object2 = { | |
| one: 1, | |
| two: 2 | |
| }; | |
| object3 = { | |
| one: 1, | |
| two: 2 | |
| }; | |
| User.create({ | |
| name: "John Smith" | |
| }); |
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
| favorite_color = "Blue (or) Red ..." | |
| question = "Bridgekeeper: what ...is your favorite color? | |
| Galahad: #{favorite_color} | |
| Bridgekeeper:wrong! | |
| " |
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
| var favorite_color, question; | |
| favorite_color = "Blue (or) Red ..." | |
| question = "Bridgekeeper: What... is your favourite color? Galahad: " + favourite_color + " Bridgekeeper: Wrong! "; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment