# Object.defineProperty(Class.prototype, 'constructor', ... vs Class.prototype.constructor = Class

**URL:** <https://discourse.mozilla.org/t/object-defineproperty-class-prototype-constructor-vs-class-prototype-constructor-class/89490>\
**Category:** Learn\
**Created:** [November 28, 2021, 2:59am UTC](https://discourse.mozilla.org/t/object-defineproperty-class-prototype-constructor-vs-class-prototype-constructor-class/89490 "2021-11-28T02:59:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![horusofoz](https://avatars.discourse-cdn.com/v4/letter/h/6a8cbe/32.png) [@horusofoz](https://discourse.mozilla.org/u/horusofoz)\
**Post date:** [November 28, 2021, 2:59am UTC](https://discourse.mozilla.org/t/object-defineproperty-class-prototype-constructor-vs-class-prototype-constructor-class/89490/1 "2021-11-28T02:59:57Z")

</div>

In the [Inheritance article](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance) for JavaScript Objects module, the instructions and example code advise to use the following snippet to update the `Teacher.prototype.constructor` value from `Person` to `Teacher` after setting the prototype to `Person`.

```auto
Object.defineProperty(Teacher.prototype, 'constructor', {
    value: Teacher,
    enumerable: false, // so that it does not appear in 'for in' loop
    writable: true });

```

Later near the end of the [Trying the example out](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance#trying_the_example_out) section, the [finished version](https://github.com/mdn/learning-area/blob/master/javascript/oojs/advanced/oojs-class-inheritance-finished.html) code uses this snippet as expected.

However a short while later in the [finished version](https://github.com/mdn/learning-area/blob/master/javascript/oojs/advanced/oojs-class-inheritance-student.html) of the [A further exercise](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance#a_further_exercise) section, the Teacher.prototype.constructor value and that of the new Student class are set using a different method.

```auto
Teacher.prototype.constructor = Teacher;
...
Student.prototype.constructor = Student;

```

Both approaches appear to have the same result in the console however I can’t find the explanation for the second method. I can follow the logic easily enough but unsure if I missed a comparison of the two ways of setting the Class/Constructor.prototype.constructor property. Is there one I overlooked?

---

<div class="post-metadata">

**Author:** ![mikoMK](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/mikomk/32/50456_2.png) [@mikoMK](https://discourse.mozilla.org/u/mikoMK)\
**Post date:** [November 28, 2021, 3:29am UTC](https://discourse.mozilla.org/t/object-defineproperty-class-prototype-constructor-vs-class-prototype-constructor-class/89490/2 "2021-11-28T03:29:03Z")

</div>

Hello @horusofoz

The difference lays in the default values of `configurable`, `enumerable` and `writable`. There’s a comparison on the [Object.defineProperty() page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#adding_properties_and_default_values).

In the second version the property would, for example, be enumerable, which isn’t probably what you want, but i think this version was used to make it look cleaner and it doesn’t really matter for this exercise.

Cheers,  
Michael

---

<div class="post-metadata">

**Author:** ![horusofoz](https://avatars.discourse-cdn.com/v4/letter/h/6a8cbe/32.png) [@horusofoz](https://discourse.mozilla.org/u/horusofoz)\
**Post date:** [November 28, 2021, 4:54am UTC](https://discourse.mozilla.org/t/object-defineproperty-class-prototype-constructor-vs-class-prototype-constructor-class/89490/3 "2021-11-28T04:54:44Z")

</div>

Thank you @mikoMK for the explanation. A quick follow up. What scenarios would you want to prevent a property from being enumerable? If RAFO, feel free to say so.

---

<div class="post-metadata">

**Author:** ![mikoMK](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/mikomk/32/50456_2.png) [@mikoMK](https://discourse.mozilla.org/u/mikoMK)\
**Post date:** [November 28, 2021, 5:22am UTC](https://discourse.mozilla.org/t/object-defineproperty-class-prototype-constructor-vs-class-prototype-constructor-class/89490/4 "2021-11-28T05:22:13Z")

</div>

The `constructor` property is actually a good example of a “not-wanting-to-be-enumerable” property and the comment on your first code block above hints at the reason:  
Think about using objects with arbitrary “key-value” pairs. They inherit some specialized functions from a parent object. Now, when you are using a `for..in` loop to loop over those properties you probably don’t want those functions and the constructor to be included.
