1

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();

JSbin link here

Jyoti Prakash
  • 1,180
  • 8
  • 9
  • Check this answer ... > http://stackoverflow.com/questions/4166616/understanding-the-difference-between-object-create-and-new-somefunction – Atul Sharma Dec 27 '16 at 10:07

0 Answers0