Last active
February 27, 2019 15:03
-
-
Save joewright/54ed18a7ee039943d74b958b704e3ea4 to your computer and use it in GitHub Desktop.
Prototype example
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
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