Created
April 23, 2012 02:51
-
-
Save jomontanari/2468503 to your computer and use it in GitHub Desktop.
Code snippets for ThoughtWorks Team Hug presentation - comparing JavaScript and CoffeeScript (part 3)
This file contains 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 objectToString = function (obj) { | |
var key, val, _results; | |
_results = []; | |
for (key in obj) { | |
if (!obj.hasOwnProperty(key)) continue; | |
val = obj[key]; | |
if (val !== null) _results.push(key + ":" + val); | |
} | |
return _results.join(","); | |
}; |
This file contains 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
objectToString = (obj) -> | |
("#{key}:#{val}" for own key, val of obj when val isnt null).join(“,") |
This file contains 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 Region = function(states) { | |
this.states = states; | |
}; | |
Region.prototype.findStatesBeginningWith = function(letter) { | |
var matchingStates = []; | |
for (var i = 0;i < this.states.length; i++) { | |
state = this.states[i]; | |
if (state.substr(0,1) === letter) { | |
matchingStates.push(state) | |
} | |
} | |
return matchingStates; | |
}; |
This file contains 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
class Region | |
constructor: (@states) -> | |
findStatesBeginningWith: (letter) -> | |
state for state in @states when state.substr(0,1) is letter |
This file contains 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 Clickable = function (baseElement) { | |
var that = this; | |
this.displayAlert = function() { | |
alert("You just clicked me!"); | |
}; | |
$(baseElement).click(that.displayAlert); | |
}; |
This file contains 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
class window.Clickable | |
constructor: (@baseElement) -> | |
$(@baseElement).click(@displayAlert) | |
displayAlert: => | |
window.alert("You just clicked me!") |
This file contains 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 london = { | |
lat: 51.5171, | |
lng: 0.1062 | |
}; | |
var lat = london.lat; | |
var lng = london.lng; | |
This file contains 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
london = | |
lat: 51.5171 | |
lng: 0.1062 | |
{lat,lng} = london |
This file contains 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
function doSomethingWithPoint(point) { | |
lat = point.lat; | |
lng = point.lng; | |
console.log(lat, lng); | |
} | |
doSomethingWithPoint(london); |
This file contains 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
doSomethingWithPoint = ({lat,lng}) -> | |
console.log(lat, lng); | |
doSomethingWithPoint london |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment