Created
June 3, 2012 01:26
-
-
Save skusunam/2860845 to your computer and use it in GitHub Desktop.
functions, aliases and existential operator
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
@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 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
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(); | |
} |
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
# 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) | |
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
// 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