0

So, I want to know the difference between the following code blocks.I found that there's nothing different at all when I define some business logic module.

function People(){}
People.prototype = {role : 'user'}
function Male(){}
Male.prototype = new People(); // the difference
var m = new Male()
m.role // print user
m instanceof Male   // print true


function People(){}
People.prototype = {role : 'user'}
function Male(){}
Male.prototype = People.prototype; // the difference
var m = new Male()
m.role // print user
m instanceof People // print true
Zhang Yang
  • 91
  • 3
  • There should be a difference when you assign values to `this` inside the `People` constructor. – Ja͢ck Jun 05 '15 at 02:44
  • In the 2nd, `Male` and `People` share the exact same object as their `prototype`. Doing that, one isn't inheriting from the other. – Jonathan Lonowski Jun 05 '15 at 02:46
  • In the first case, *Male.prototype* and *People.prototype* are separate objects, and while changes to *People.prototype* are inherited by instances of *Male*, changes to *Male.prototype* are not inherited by instances of *People*, so assigning a method like *beatChest* to *Male.prototype* means only *males* can beat their chest. In the second case, *Male.prototype* and *People.prototype* are the same object, so changes are inherited by instances of both *People* and *Male*, so all *people* and *males* could beat their chest. – RobG Jun 05 '15 at 03:13

0 Answers0