Let's say I have the following code:
(function($) {
var Obj = {
init: function() {
var c1 = Object.create(this.MyChild);
var c2 = Object.create(this.MyChild);
c1.init(); //not working!!!
},
MyChild: function() {
this.init = function() {
console.log('calling MyChild init function');
}
}
};
Obj.init();
})(jQuery);
When creating Obj, I used object literal as I don't need to create the instance of it, and when creating MyChild objects, I used the constructor function and used Object.create as I need to create multiple instances of MyChild.
However, when I call Object.create, it doesn't work, when c1.init() is called, it says the init function is undefined, but if I replaced Object.create(this.MyChild) to:
var c1 = new this.MyChild();
c1.init();
why?