Object.create(Function.prototype)和new Function()之间有什么区别

时间:2016-12-27 10:04:41

标签: javascript oop inheritance

查看此代码。这里我试图从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();

JSbin link here

0 个答案:

没有答案