# Constructor property of objects created from a constructor function

**URL:** <https://forum.freecodecamp.org/t/constructor-property-of-objects-created-from-a-constructor-function/409102>\
**Category:** freeCodeCamp Contributors\
**Tags:** contributing\
**Created:** [July 11, 2020, 4:16pm UTC](https://forum.freecodecamp.org/t/constructor-property-of-objects-created-from-a-constructor-function/409102 "2020-07-11T16:16:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![selectiveduplicate](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/selectiveduplicate/32/154483_2.png) [@selectiveduplicate](https://forum.freecodecamp.org/u/selectiveduplicate)\
**Post date:** [July 11, 2020, 4:16pm UTC](https://forum.freecodecamp.org/t/constructor-property-of-objects-created-from-a-constructor-function/409102/1 "2020-07-11T16:16:41Z")

</div>

Let’s say I have a constructor function:

```auto
function Bird(name) {
  this.name = name;
}

```

Now I create an object named `duck`.

```auto
let duck = new Bird("Donald");

```

Now the following quick commands and their outputs:

```auto
duck.constructor === Object; // returns false

```

According to [this](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/object-oriented-programming/remember-to-set-the-constructor-property-when-changing-the-prototype) section of our module, this should return true.

I get this to be true only if I create object manually, i.e. without using any constructor:

```auto
let something = {};
something.constructor === Object; // returns true

```

This actually makes sense rather than `duck.constructor === Object` returning true since `duck` wasn’t created using the `Object()` constructor. Sure, it inherits features defined in `Object.prototype`, but the constructor isn’t the same.

This is clearly mentioned in MDN [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor#Description):

> Objects created without the explicit use of a constructor function (such as object- and array-literals) will have a `constructor` property that points to the Fundamental Object constructor type for that object.

Pardon me if I’ve missed something about the issue and posted in the wrong category. Thanks!

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [July 11, 2020, 4:45pm UTC](https://forum.freecodecamp.org/t/constructor-property-of-objects-created-from-a-constructor-function/409102/2 "2020-07-11T16:45:07Z")

</div>

I’m no expert on this. but the way that I understand it is that _Object_ is the constructor. So, that is the default constructor for any object you create. So, when I write this code:

```auto
function Bird(name) {
  this.name = name;
}

let duck = new Bird("Donald");
console.log('duck.constructor -->', duck.constructor);
console.log('duck.constructor === Object -->', duck.constructor === Object);

console.log('Object -->', Object);

let something = {};
console.log('something.constructor -->', something.constructor);
console.log('something.constructor === Object -->', something.constructor === Object);

```

I get this output:

```auto
duck.constructor --> ƒ Bird(name) {
  this.name = name;
}

duck.constructor === Object --> false

Object --> ƒ Object() { [native code] }

something.constructor --> ƒ Object() { [native code] }

something.constructor === Object --> true

```

So, it seems that _Object_ and the default constructor are (presumably) the same thing. In the case of _Bird_, it got overwritten with your code.

And notice that if I log out _Bird_ I get:

```auto
Bird --> ƒ Bird(name) {
  this.name = name;
}

```

This is identical to _duck.contructor_. So, both _Bird_ and _Object_ are constructors and they just get stored in the objects they construct.

Does that makes sense? Again, this isn’t my expertise, but this is how I read it.

If you like this kind of stuff, I recommend the YDKJS book series.

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [July 11, 2020, 4:58pm UTC](https://forum.freecodecamp.org/t/constructor-property-of-objects-created-from-a-constructor-function/409102/3 "2020-07-11T16:58:20Z")

</div>

Just to reiterate what @kevinSmith said:

With this:

```auto
duck.constructor === Bird; // false -- Oops
duck.constructor === Object; // true, all objects inherit from Object.prototype

```

The lesson means to prove you can have either or. So, without setting the constructor property:

```auto
Bird.prototype = {
  constructor: Bird, // define the constructor property
  numLegs: 2,

```

It is as the first code snippet mentions. However, after setting the constructor, this becomes what we want:

```auto
duck.constructor === Bird; // true-- Yay!
duck.constructor === Object; // false, cannot be both

```

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [July 11, 2020, 5:21pm UTC](https://forum.freecodecamp.org/t/constructor-property-of-objects-created-from-a-constructor-function/409102/4 "2020-07-11T17:21:56Z")

</div>

The point of the challenge (as far as I can tell) is to show that when you define the prototype, like it was done in the [previous challenge](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/object-oriented-programming/change-the-prototype-to-a-new-object), you overwrite the constructor. So you need to set it manually. I might just be repeating what was already said to be honest.

```auto
function Bird() {
  this.name = "Albert";
  this.color = "blue";
  this.numLegs = 2;
}

let duck = new Bird('Jack')
console.log(duck.constructor === Object); // false
console.log(duck.constructor === Bird); // true

Bird.prototype = {
  numLegs: 2, 
  eat: function() {
    console.log("nom nom nom");
  },
  describe: function() {
    console.log("My name is " + this.name);
  }
};

duck = new Bird('Jack')
console.log(duck.constructor === Object); // true
console.log(duck.constructor === Bird); // false

Bird.prototype = {
  constructor: Bird,
  numLegs: 2, 
  eat: function() {
    console.log("nom nom nom");
  },
  describe: function() {
    console.log("My name is " + this.name);
  }
};

duck = new Bird('Jack')
console.log(duck.constructor === Object); // false
console.log(duck.constructor === Bird); // true

```

---

<div class="post-metadata">

**Author:** ![selectiveduplicate](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/selectiveduplicate/32/154483_2.png) [@selectiveduplicate](https://forum.freecodecamp.org/u/selectiveduplicate)\
**Post date:** [July 11, 2020, 6:00pm UTC](https://forum.freecodecamp.org/t/constructor-property-of-objects-created-from-a-constructor-function/409102/5 "2020-07-11T18:00:29Z")

</div>

```auto
duck.constructor === Object

```

always returns `false` for any object created with constructor function, whether its constructor property is set or not.

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [July 11, 2020, 10:01pm UTC](https://forum.freecodecamp.org/t/constructor-property-of-objects-created-from-a-constructor-function/409102/6 "2020-07-11T22:01:37Z")

</div>

Run this code to see what I mean:

```auto
function Dog(name) {
  this.name = name;
}

// Only change code below this line
Dog.prototype = {
  numLegs: 4,
  eat: function() {
    console.log("nom nom nom");
  },
  describe: function() {
    console.log("My name is " + this.name);
  }
};

const woof = new Dog("Woot");
console.log(woof.constructor === Object)

```

---

<div class="post-metadata">

**Author:** ![selectiveduplicate](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/selectiveduplicate/32/154483_2.png) [@selectiveduplicate](https://forum.freecodecamp.org/u/selectiveduplicate)\
**Post date:** [July 13, 2020, 8:44am UTC](https://forum.freecodecamp.org/t/constructor-property-of-objects-created-from-a-constructor-function/409102/7 "2020-07-13T08:44:49Z")

</div>

So after setting an object’s prototype to an object it overwrites the constructor property to `Object` instead and so now `woof.constructor === Bird` is false but `woof.constructor === Object` is true. Am I stating that right?

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [July 13, 2020, 11:52am UTC](https://forum.freecodecamp.org/t/constructor-property-of-objects-created-from-a-constructor-function/409102/8 "2020-07-13T11:52:48Z")

</div>

This is how I interpret it:

> So after setting an object’s constructor to an object it overwrites the constructor property to **that object** instead and so now `woof.constructor === Dog` is true but `woof.constructor === Object` is false.

That is, the default is `variable.constructor === Object`. Only once this is explicitly overwritten, does this not become true. However, this appears to only be precise for class objects (something declared with keyword `class` or inherit with prototypes.)

It is very _case specific_.

---

<div class="post-metadata">

**Author:** ![Lord\_Basilisk](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lord_basilisk/32/172748_2.png) [@Lord\_Basilisk](https://forum.freecodecamp.org/u/Lord_Basilisk)\
**Post date:** [August 9, 2020, 8:27am UTC](https://forum.freecodecamp.org/t/constructor-property-of-objects-created-from-a-constructor-function/409102/9 "2020-08-09T08:27:11Z")

</div>

I am a little bit confused with two different syntax of defining a constructor. In “Learn ES6”, we defined constructors using the “constructor” keyword. And now in this challenge, in OOP, we are defining constructors using the “function” keyword.  
I already asked this question in FreeCodeCamp group on Facebook but I didn’t get what they said except that one can inherit and the other one can’t.

Can someone please explain this in simple terms but with all the details ?

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [August 9, 2020, 10:13am UTC](https://forum.freecodecamp.org/t/constructor-property-of-objects-created-from-a-constructor-function/409102/10 "2020-08-09T10:13:09Z")

</div>

```auto
function User (name) {
  this.name = name;
}

User.prototype.sayHello = function () {
  return `hello, my name is ${this.name}`;
}

```

Same thing:

```auto
class User {
  constructor (name) {
    this.name = name;
  }

  sayHello() {
    return `hello, my name is ${this.name}`;
  }
}

```

Like, literally the same, both are used the same way:

```auto
let user1 = new User("DanCouper");

```

The `class` syntax was added in 2015, it does exactly the same as the first example, but

1. it’s arguably clearer,
2. it visually encapsulates all the related functionality,
3. it’s easier to understand for programmers coming to JS from other languages with classes,
4. adds some safeguards to prevent common errors,
5. allows inheriting from builtins, which I think it’s what someone had probably mentioned but isn’t very important (you can still normally use inheritance fine with the first example)

---

<div class="post-metadata">

**Author:** ![Lord\_Basilisk](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lord_basilisk/32/172748_2.png) [@Lord\_Basilisk](https://forum.freecodecamp.org/u/Lord_Basilisk)\
**Post date:** [August 9, 2020, 10:30am UTC](https://forum.freecodecamp.org/t/constructor-property-of-objects-created-from-a-constructor-function/409102/11 "2020-08-09T10:30:31Z")

</div>

Thank you for the clarification.  
You defined it better than those Facebook guys.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:32pm UTC](https://forum.freecodecamp.org/t/constructor-property-of-objects-created-from-a-constructor-function/409102/12 "2021-06-01T18:32:30Z")

</div>

Closed on June 1, 2021, 6:32pm UTC


