Skip to content

Instantly share code, notes, and snippets.

@joewright
Last active February 27, 2019 15:03
Show Gist options
  • Save joewright/54ed18a7ee039943d74b958b704e3ea4 to your computer and use it in GitHub Desktop.
Save joewright/54ed18a7ee039943d74b958b704e3ea4 to your computer and use it in GitHub Desktop.
Prototype example
var DB = {};
function Base(data) {
for (var key in data) {
this[key] = data;
}
}
Base.prototype.save = function() {
DB[Math.random().toString()] = this.serialize();
};
Base.prototype.serialize = function() {
var output = {};
for (var key in this) {
if (this[key] instanceof Function === false) {
output[key] = this[key];
}
}
return output;
};
function Sub(data) {
Base.call(this, data);
}
Sub.prototype = Object.create(Base.prototype);
Sub.prototype.woo = function() {
console.log('hoo');
};
Sub.prototype.whatever = function() {};
var heh = new Sub({
lmao: true
});
heh.save();
heh.woo();
console.log(DB);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment