Skip to content

Instantly share code, notes, and snippets.

@nanodeath
Created December 12, 2010 01:55
Show Gist options
  • Save nanodeath/737780 to your computer and use it in GitHub Desktop.
Save nanodeath/737780 to your computer and use it in GitHub Desktop.
mixin DumbEqualsOperand
equals: (other) ->
this == other
class MyClass implements DumbEqualsOperand
m = new MyClass
n = new MyClass
console.log(m.equals n)
(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);
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