Clarification on Prototypes and not Repeating Oneself

This is what FCC says:

Since numLegs will probably have the same value for all instances of Bird, you essentially have a duplicated variable numLegs inside each Bird instance.

This may not be an issue when there are only two instances, but imagine if there are millions of instances. That would be a lot of duplicated variables.

To this problem, I need to write Bird.prototype.numLegs() = 4. However, I thought the following code would eliminate the need to repeat yourself:

function Bird (name, color) {
this.name = name;
this.color = color;
this.feet = 2
}

var parrot = new Bird ('Bill', 'multi-colored') 
var hawk = new Bird ('Death', 'black') 
var blueJay = new Bird ('Tom', 'blue')
console.log(parrot) // -- > Bird { name: 'Bill', color: 'multi-colored', feet: 2}
console.log(hawk) // -- > Bird { name: 'Death', color: 'black', feet: 2}
console.log(blueJay) // -- > Bird { name: 'Tom', color: 'blue', feet: 2}

As you can see, I don’t have to repeat myself saying that I want every Bird to have 2 feet, since I didn’t give it a parameter. As FCC said though, is that in order not to repeat myself I’m going to need to use .prototype() to do this. Why is this the case? I didn’t have to repeat myself in that last section of code.

Is this in reference to a particular challenge? If so please provide the link.

Here you go:

I’m just confused on the explanation. I was told that we use new and make constructors so that we can reuse objects, but now it’s saying that, when we have a property that doesn’t change like the one in the freeCodeCamp example, we need to make a new prototype.

It is saying that when creating the variable (property) on the instance, each instance will create its own variable. So instead, we can use the prototype property to share this across instances. Each instance will look up the prototype chain for the property. So all instances can share the same property/properties.

1 Like

Thank you. So whenever I want to make a constructor and I want a property to be just one thing (like numLegs just being 4), then I would use prototype?

Say we do this:

function Bird(name, color){
  this.name = name;
  this.color = color;
}
Bird.prototype.numLegs = 2;
Bird.prototype.print = function(){
  return "Hi, I'm "+this.name+", a "+this.color+" bird with "+this.numLegs+" legs!";
}

const bob = new Bird("Bob", "yellow");
console.log(bob.print() );

Now this.numLegs is not currently defined in Bob. Bob has no legs of his own. If you did bob.hasOwnProperty("numLegs") it will be false - Bob has no legs. But because he’s a Bird, he can access the numLegs in his prototype chain. So even though he has no legs of his own, he can borrow them from Bird.

But what if Bob had an accident, a run-in with a caiman? Well, the default no longer applies, so we can:

bob.numLegs = 1.2;

And with this, Bob no longer knows about the default value in Bird, he has his own legs. Doing this, we’ve increased the amount of data stored for Bob, as he no longer has that shared property - but he has the advantage of customization, and a little birdie peg-leg. If that caiman had taken the legs of millions of birds, in whole or in part, then all those Bird instances would take a little more space each. But many small space increases would accumulate to a BIG one. And a full caiman.

Suppose we later found that Bob had committed Medicare fraud, faked the leg thing - he had the proper birdie number of legs all along! His girlfriend thought the peg-leg was stylish, so Bob… Well he doesn’t need the custom leg value. We can still:

delete bob.numLegs;

And with that, he becomes a little smaller, a little less custom…but can now access that shared value from Bird again.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.