Skip to content

Instantly share code, notes, and snippets.

@munro
Created January 6, 2012 21:16
Show Gist options
  • Save munro/1572444 to your computer and use it in GitHub Desktop.
Save munro/1572444 to your computer and use it in GitHub Desktop.
Phredbob Inheritance
function algorithm(v) {
if (v === 0) {
return 'a';
} else if (v === 1) {
return 'b';
} else if (v === 2) {
return 'c';
}
}
var tab = {
Hello: {
someMethod: function (v) {
if (v === 0) {
return false;
} else {
return algorithm(v);
}
}
},
World: {
someMethod: function (v) {
if (v === 1) {
return false;
} else {
return algorithm(v);
}
}
}
};
function Base() {
}
Base.prototype.someMethod = function (v) {
if (v === 0) {
return 'a';
} else if (v === 1) {
return 'b';
} else if (v === 2) {
return 'c';
}
}
function Hello() {
}
Hello.prototype = Object.create(Base.prototype);
Hello.prototype.someMethod = function (v) {
if (v === 0) {
return false;
} else {
return Base.prototype.someMethod.call(this, v);
}
};
function World() {
}
World.prototype = Object.create(Base.prototype);
World.prototype.someMethod = function (v) {
if (v === 1) {
return false;
} else {
return Base.prototype.someMethod.call(this, v);
}
};
var tab = {
Hello: new Hello(),
World: new World()
};
var Base = Self({
someMethod: function (self, v) {
if (v === 0) {
return 'a';
} else if (v === 1) {
return 'b';
} else if (v === 2) {
return 'c';
}
}
});
var Hello = Base.extend({
someMethod: function (self, v) {
if (v === 0) {
return false;
} else {
return Hello.__super__.someMethod.call(self, v);
}
}
});
var World = Base.extend({
someMethod: function (self, v) {
if (v === 1) {
return false;
} else {
return World.__super__.someMethod.call(self, v);
}
}
});
var tab = {
Hello: Hello(),
World: World()
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment