Skip to content

Instantly share code, notes, and snippets.

@devopsmariocom
Last active December 17, 2015 06:08
Show Gist options
  • Save devopsmariocom/5563064 to your computer and use it in GitHub Desktop.
Save devopsmariocom/5563064 to your computer and use it in GitHub Desktop.
Javascript vs CoffeScript
if x? then use(x)
cupsOfCoffee ?= 0
coffeePot?.brew()
if (typeof x !== "undefined" && x !== null) {
use(x);
}
if (cupsOfCoffee == null) {
cupsOfCoffee = 0;
}
if (typeof coffeePot !== "undefined" && coffeePot !== null) {
coffeePot.brew();
}
var developers;
developers = {
mario: {
xxx: 'yyy'
},
alex: {
jjj: 'www'
},
beguene: {
ttt: 'rrr'
},
greeting: function() {
return alert('hey!');
}
};
developers =
mario:
xxx:
'yyy'
alex:
jjj:
'www'
beguene:
ttt:
'rrr'
greeting: ->
alert 'hey!'
# This will simply create array of attributes names
attributes_name = (attribute.name for attribute in Attribute.all())
# run console.log for each attribute name
console.log attribute.name for attribute in Attribute.all()
# run console.log for each split attribute.name
console.log attribute.name for attribute in Attribute.all() when attribute.is_split?
var attribute, attributes_name;
attributes_name = (function() {
var _i, _len, _ref, _results;
_ref = Attribute.all();
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
attribute = _ref[_i];
_results.push(attribute.name);
}
return _results;
})();
_ref = Attribute.all();
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
attribute = _ref[_i];
console.log(attribute.name);
}
var attribute, _i, _len, _ref;
_ref = Attribute.all();
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
attribute = _ref[_i];
if (attribute.is_split != null) {
console.log(attribute.name);
}
}
class Coffee
constructor: (@name, @strength=1) ->
brew: -> alert "brewing #{@name}"
class MaxgoodHouse extends Coffee
constructor: (@name, @strength=0) ->
@brand = "Maxgood House"
boring = new MaxgoodHouse("Boring")
boring.brew()
var Coffee, MaxgoodHouse, boring,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
Coffee = (function() {
function Coffee(name, strength) {
this.name = name;
this.strength = strength != null ? strength : 1;
}
Coffee.prototype.brew = function() {
return alert("brewing " + this.name);
};
return Coffee;
})();
MaxgoodHouse = (function(_super) {
__extends(MaxgoodHouse, _super);
function MaxgoodHouse(name, strength) {
this.name = name;
this.strength = strength != null ? strength : 0;
}
MaxgoodHouse.brand = "Maxgood House";
return MaxgoodHouse;
})(Coffee);
boring = new MaxgoodHouse("Boring");
boring.brew();
for i in [0..10]
console.log i
#slicing
myArray[2..5]
for (i = _j = 0; _j <= 10; i = ++_j) {
console.log(i);
}
myArray.slice(2, 6);
# no awkward '+' joining
error ( message ) ->
alert "Hey here is #{message}"
error(function(message) {
return alert("Hey here is " + message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment