Skip to content

Instantly share code, notes, and snippets.

@skusunam
Created June 3, 2012 01:26
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/2860845 to your computer and use it in GitHub Desktop.
Save skusunam/2860845 to your computer and use it in GitHub Desktop.
functions, aliases and existential operator
@saviour = true
# prototype
User::first = ->@record[0]
# existential operator (?)
praise if brain?
# existential operator (?) in place of || operator
velocity = southern ? 40
# If you're using a null check before accessing a property, you can skip that by placing the existential operator right before it
blackKnight.getLegs()?.kick()
#Similarly you can check that a property is actually a function, and callable, by placing the existential operator right before the parens. If the property doesn't exist, or isn't a function, it simply won't get called.
blackKnight.getLegs().kick?()
this.saviour = true;
// prototype
User.prototype.first = function() {
return this.records[0];
};
// existential operator (?)
if (typeof brian !== "undefined" && brian !== null) {
praise;
}
// existential operator (?) in place of || operator
var velocity;
velocity = typeof southern !== "undefined" && southern !== null ? southern : 40;
/* If you're using a null check before accessing a property, you can skip that by placing the # existential operator right before it */
var _ref;
if ((_ref = blackKnight.getLegs()) != null) {
_ref.kick();
}
/* Similarly you can check that a property is actually a function, and callable, by placing the existential operator right before the parens. If the property doesn't exist, or isn't a function, it simply won't get called. */
var _base;
if (typeof (_base = blackKnight.getLegs()).kick === "function") {
_base.kick();
}
# default arguments
func = (a = 1, b = 2) -> a * b
# splats to accept multiple arguments
sum = (nums...) ->
result = 0
nums.forEach (n) -> result += n
result
# function invocation (If there are no arguments, () are mandatory)
a = "Howdy!"
alert a
alert (a)
# function context (fat arrow => this ensures that the function context will be bound to the local one)
@clickHandler = -> alert "clicked"
element.addEventListener "click", (e) => @clickHandler(e)
// default arguments
var func;
func = function(a, b) {
if ( a == null) {
a = 1;
}
if ( b == null) {
b = 1;
}
return a * b;
};
// splats to accept multiple arguments
var sum;
var __slice = Array.prototype.slice;
sum = function() {
var nums, result;
nums = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
result = 0;
nums.forEach(function(n) {
return result += n;
});
return result;
};
// function invocation (If there are no arguments, () are mandatory)
var a;
a = "Howdy!"
alert (a)
alert (a)
// function context (fat arrow => this ensures that the function context will be bound to the local one)
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
this.clickHandler = function() {
return alert("clicked");
};
element.addEventListener("click", __bind(function(e) {
return this.clickHandler(e);
}, this));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment