Skip to content

Instantly share code, notes, and snippets.

@EvanHahn
EvanHahn / gist:3364322
Created August 15, 2012 22:35
Inheriting private member functions working in CoffeeScript, with .call()
# This is an example in a post about private members in CoffeeScript.
# Read more: http://evanhahn.com/?p=1126
class Sorcerer
constructor: (@spell) ->
conjureSpell = -> # private
@spell.conjure()
useSpell: ->
conjureSpell.call(this)
@spell.use()
@EvanHahn
EvanHahn / gist:3364303
Created August 15, 2012 22:30
Getting private member functions working in CoffeeScript by defining them in the prototype breaks inheritance
# This is an example in a post about private members in CoffeeScript.
# Read more: http://evanhahn.com/?p=1126
class Sorcerer
conjureSpell = null
constructor: (@spell) ->
conjureSpell = => # private
@spell.conjure()
useSpell: ->
conjureSpell()
@EvanHahn
EvanHahn / gist:3364224
Created August 15, 2012 22:17
Getting private member functions working in CoffeeScript by scoping differently breaks inheritance
# This is an example in a post about private members in CoffeeScript.
# Read more: http://evanhahn.com/?p=1126
class Sorcerer
that = null
constructor: (@spell) ->
that = this
conjureSpell = -> # private
that.spell.conjure()
useSpell: ->
@EvanHahn
EvanHahn / gist:3364153
Created August 15, 2012 22:11
Getting private member functions working in CoffeeScript, by scoping differently
# This is an example in a post about private members in CoffeeScript.
# Read more: http://evanhahn.com/?p=1126
class Sorcerer
that = null
constructor: (@spell) ->
that = this
conjureSpell = -> # private
that.spell.conjure() # Note this hack
useSpell: ->
@EvanHahn
EvanHahn / gist:3364133
Created August 15, 2012 22:08
Getting private member functions working in CoffeeScript, with .call()
# This is an example in a post about private members in CoffeeScript.
# Read more: http://evanhahn.com/?p=1126
class Sorcerer
constructor: (@spell) ->
conjureSpell = -> # private
@spell.conjure()
useSpell: ->
conjureSpell.call(this) # Note this little hack
@spell.use()
@EvanHahn
EvanHahn / gist:3363965
Created August 15, 2012 21:41
Simple private member functions in CoffeeScript
# There's more complexity to this, as described below:
# http://evanhahn.com/?p=1126
class Person
hello = ->
"hello"
helloWorld: ->
"#{hello()} world!"
me = new Person
@EvanHahn
EvanHahn / gist:3330517
Created August 12, 2012 07:42
JavaScript sleepsort
// JavaScript sleepsort
// by Evan Hahn (evanhahn.com)
// Port of this genius on 4chan:
// http://dis.4chan.org/read/prog/1295544154
// Call it like this:
// sleepsort(5, 4, 12, 8, 9, 10, 240);
function sleepsort() {
@EvanHahn
EvanHahn / gist:3309613
Created August 10, 2012 00:38
Strip HTML tags
var tagsStripped = str.replace(/<[^>]*>/g, '');
@EvanHahn
EvanHahn / gist:2955704
Last active October 6, 2015 07:08
Instead of this, use Paul Miller's solution: https://github.com/paulmillr/console-polyfill
# Make console methods no-ops in unsupported browsers, terse
# More versions: <http://evanhahn.com/?p=990>
noop = ->
window.console || (window.console = {})
window.console.error || (window.console.error = noop)
window.console.info || (window.console.info = noop)
window.console.log || (window.console.log = noop)
window.console.warn || (window.console.warn = noop)
@EvanHahn
EvanHahn / gist:2955696
Last active October 6, 2015 07:07
Instead of this, use Paul Miller's solution: https://github.com/paulmillr/console-polyfill
# Make console methods no-ops in unsupported browsers, complete
# More versions: <http://evanhahn.com/?p=990>
noop = ->
window.console || (window.console = {})
window.console.assert || (window.console.assert = noop)
window.console.clear || (window.console.clear = noop)
window.console.count || (window.console.count = noop)
window.console.debug || (window.console.debug = noop)
window.console.dir || (window.console.dir = noop)