Created
December 12, 2010 01:55
-
-
Save nanodeath/737780 to your computer and use it in GitHub Desktop.
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
mixin DumbEqualsOperand | |
equals: (other) -> | |
this == other | |
class MyClass implements DumbEqualsOperand | |
m = new MyClass | |
n = new MyClass | |
console.log(m.equals n) | |
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
(function(){ | |
var DumbEqualsOperand, MyClass, m, n; | |
var __hasProp = Object.prototype.hasOwnProperty, __implements = function(child, mixin) { | |
for (var key in mixin) { if (__hasProp.call(mixin, key)) child[key] = mixin[key]; } | |
}; | |
// there's actually no reason to implement mixins as classes instead of simple hashes as far as I can tell, but am doing so for consistency | |
DumbEqualsOperand = function(){ | |
function DumbEqualsOperand(){} | |
DumbEqualsOperand.prototype.equals = function(other){ | |
return this == other; | |
}; | |
return DumbEqualsOperand; | |
}(); | |
MyClass = function(){ | |
MyClass = function(){}; | |
_implements(MyClass, DumbEqualsOperand); | |
}(); | |
m = new MyClass(); | |
n = new MyClass(); | |
console.log(m.equals(n)); | |
}).call(this); |
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
function BaseError(){} | |
function AnnoyingError(){} | |
AnnoyingError.prototype = BaseError; | |
function BothersomeError(){} | |
BothersomeError.prototype = BaseError; | |
function errorProne(idx){ | |
if(idx < 0){ | |
throw new AnnoyingError(); | |
} else if(idx > 1){ | |
throw new BothersomeError(); | |
} | |
} | |
try { | |
errorProne(-2); | |
} catch(e){ | |
if(e instanceof AnnoyingError){ | |
console.log("that was annoying"); | |
} else if(e instanceof BothersomeError){ | |
console.log("that was bothersome"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment