Created
June 10, 2016 18:24
-
-
Save crc442/892b0858b0e992ab9b60e1ca954c3ba2 to your computer and use it in GitHub Desktop.
This file contains 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
// 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