Created
July 27, 2013 13:09
-
-
Save mikemaccana/6094826 to your computer and use it in GitHub Desktop.
Simple inheritance with Object.create() and a factory function
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 makeObject = function(prototype, properties) { | |
var newObject = Object.create(prototype || null) // If no prototype is set, just create a regular object literal (which inherits from null) | |
for ( var property in properties ) { | |
if ( properties.hasOwnProperty(property) ) { | |
newObject[property] = properties[property] | |
} | |
} | |
return newObject | |
} | |
var person = makeObject(null, { | |
sayHello:function(){console.log('Hi there')} | |
}) | |
var man = makeObject(person, { | |
sex: 'male' | |
}) | |
var mike = makeObject(man, { | |
firstName: 'Mike', | |
lastName: 'MacCana' | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Indeed looks very nice. How do you handle computed property values and internal instance properties? You probably know my (related) approach, which is a bit more library code, but not much:
https://github.com/rauschma/proto-js