i was answering to a question where i encountered this problem In the following code how the child's prototype can be set to parent using object.create() method.I can do it using
child.prototype=new Parent();
But i want to do it using object.create
.using child.prototype=Object.create(Parent) didn't set the prototype to Parent
function Parent() {
this.parentFunction = function(){
console.log('parentFunction');
}
}
Parent.prototype.constructor = Parent;
function Child() {
this.parentFunction = function() {
this.constructor.prototype.parentFunction.call(this);
console.log('parentFunction from child');
}
}
Child.prototype = Object.create(Parent);
Child.prototype.constructor = Child;
var child = new Child();
console.dir(child);
child.parentFunction();