Skip to content

Instantly share code, notes, and snippets.

@crc442
Created June 10, 2016 18:24
Show Gist options
  • Save crc442/892b0858b0e992ab9b60e1ca954c3ba2 to your computer and use it in GitHub Desktop.
Save crc442/892b0858b0e992ab9b60e1ca954c3ba2 to your computer and use it in GitHub Desktop.
// prototypal inheritance 'create' (utility) meathod
create: function(values) {
var instance = Object.create(this); //Object.create only in > IE8, so this can be done is only >IE8
Object.keys(values).forEach(function(key)) {
instance[key] = values[key];
}
return instance;
}
// Use it like this
var baseClass = {
value: "init value",
create: function(values) {
var instance = Object.create(this); //Object.create only in > IE8, so this can be done is only >IE8
Object.keys(values).forEach(function(key)) {
instance[key] = values[key];
}
return instance;
}
};
var childClass = baseClass.create({
someValue: "some value",
someFunction: function() {
console.log(this.someValue);
}
});
childClass.value; //init value
childClass.someValue //some value
// we can also create subclass of childClass with all properties of all parent class ans so on.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment