Look into this code. Here am trying to inherit A from B. I found many different type of solutions online. So i want to know that what is the difference between assigning the prototype
of function
with Object.create
and using new
operator.
function A(name) {
this.name = name;
}
A.prototype.getName = function() {
console.log(this.name)
}
function B(name, age) {
A.call(this, name);
this.age = age;
}
B.prototype = Object.create(A.prototype);
/*B.prototype = new A();*/
/**
* what is difference between Object.create(A.prototype) and new A()
* If i am calling any one of this two am getting the same result.
*/
B.prototype.constructor = B;
B.prototype.getInfo = function() {
console.log(this.name, this.age);
}
new B('Hello', 20).getInfo();