The constructor function with its prototype

I have done something to dig into the javascript constructor function and its details today.

So, a constructor function is set as a template to dynamically initialize the new objects with the same DNA.

I created a constructor function in my Js file called ‘Queue’. Notice that I used uppercase ‘Q’ to demonstrate a constructor function. Then, in my console, I created a new object ‘test’ by using my constructor function. Next, I printed out my test object, you can see that the newly created test object contained information from my constructor function.

Prototyoe

Now, I’m trying to print my constructor function in my console. I can see that there is a prototype key with some value in it. So, the prototype is an internal object that is initialized together with our constructor function. The cool thing is that we can use this prototype object to assign the new key with a function in it and we can use that inserted function to modify any child object.

Now, I am assigning a value to the prototype.enqueue, it should add value to my test object.

Conclusion

The takeaway from this study is that each constructor function has its prototype object. We can use that object to assign a function to it, thus, we can dynamically change the value in its child object.

Posted in Notes
Back to Top