From 56ded4174c324a2bdd8d8ac276e01650f78d6751 Mon Sep 17 00:00:00 2001 From: Aditya Date: Fri, 14 Dec 2018 16:29:35 +0530 Subject: [PATCH] [Fix] Object Oriented Programming: Remember to Set the Constructor Property when Changing the Prototype (#34569) --- ...onstructor-property-when-changing-the-prototype.english.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/remember-to-set-the-constructor-property-when-changing-the-prototype.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/remember-to-set-the-constructor-property-when-changing-the-prototype.english.md index 6df91c7014..d04a87b73d 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/remember-to-set-the-constructor-property-when-changing-the-prototype.english.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/remember-to-set-the-constructor-property-when-changing-the-prototype.english.md @@ -6,8 +6,8 @@ challengeType: 1 ## Description
-There is one crucial side effect of manually setting the prototype to a new object. It erased the constructor property! The code in the previous challenge would print the following for duck: -
console.log(duck.constructor)
// prints ‘undefined’ - Oops!
+There is one crucial side effect of manually setting the prototype to a new object. It erases the constructor property! This property can be used to check which constructor function created the instance, but since the property has been overwritten, it now gives false results: +
duck.constructor === Bird; // false -- Oops
duck.constructor === Object; // true, all objects inherit from Object.prototype
duck instanceof Bird; // true, still works
To fix this, whenever a prototype is manually set to a new object, remember to define the constructor property:
Bird.prototype = {
  constructor: Bird, // define the constructor property
  numLegs: 2,
  eat: function() {
    console.log("nom nom nom");
  },
  describe: function() {
    console.log("My name is " + this.name);
  }
};