Skip to content

Instantly share code, notes, and snippets.

@skusunam
Created June 3, 2012 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skusunam/2861548 to your computer and use it in GitHub Desktop.
Save skusunam/2861548 to your computer and use it in GitHub Desktop.
Object literals & Array definitions & String interpolations
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
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");
}
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")
var object1, object2, object3;
object1 = {
one: 1,
two: 2
};
object2 = {
one: 1,
two: 2
};
object3 = {
one: 1,
two: 2
};
User.create({
name: "John Smith"
});
favorite_color = "Blue (or) Red ..."
question = "Bridgekeeper: what ...is your favorite color?
Galahad: #{favorite_color}
Bridgekeeper:wrong!
"
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