Skip to content

Instantly share code, notes, and snippets.

@mikemaccana
Created July 27, 2013 13:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikemaccana/6094826 to your computer and use it in GitHub Desktop.
Save mikemaccana/6094826 to your computer and use it in GitHub Desktop.
Simple inheritance with Object.create() and a factory function
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'
})
@rauschma
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment