查看此代码。这里我试图从B继承A.我在网上发现了许多不同类型的解决方案。所以我想知道将prototype
function
分配给Object.create
和使用new
运算符之间有什么区别。
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();