I have been reading much about object composition in JavaScript and the advantages of this pattern over the 'class' pattern. I have seen examples of the composition that demonstrate using Object.create()
to create new objects and other examples that demonstration using factory functions that return objects.
Object.create example:
var Component = {
init: function() {
// do stuff
}
};
var button = Object.create(Component);
button.init();
Factory function example:
var ComponentFactory = function() {
return {
init: function() {
// do stuff
}
}
}
var button = ComponentFactory();
button.init();
I understand that factory functions are intended for abstracting away complexity involved in creating objects, however I am trying to understand if there is any practical difference between Object.create()
and a function which returns an object.