DEV Community

Cover image for Function Constructor in Javascript
Emmanuel Chikwendu Onah
Emmanuel Chikwendu Onah

Posted on • Edited on

Function Constructor in Javascript

Good day viewers,
today we will be looking into the functional base of class-program implementation.

The reason for this article is because many of us love to keep our code in semantic pattern that is; if we are writing functional program,we dont want to see the keyword class in it.So,we will be implementing a contructional program using function and you will never see the class keyword.

Things to learn from this article:

1.constructor

2.new keyword

3.this performance

4.prototype

5.dondo proto

6.The complete code base of the whole implementation.

  1. What is a constructor?

a constructor is just a normal javascript function with two main conventions:

a. The naming must start with capital letter

b. The execution must be implemented or invoked using the new operator or keyword.

function IamConstructor(){}
const invokingContructor = new IamConstructor();

  1. What is new all about?

The new keyword is a special and most abiding concept when implementing the object-oriented program. The this keyword creates an implicit this object inside the constructor function you created, and it will insert all the properties of the constructor to the new this object. Also, when you now create a variable and pass the new ContructorFunction to the variable, it will pass the this object to the variable you have created as a value.

const invokingContructor = new ConstructorFunction();

  1. What is the this keyword doing in our program?

I will explain this with respect to our article which is "implementation of constructional function known as class base program".

So, this creates a global property that will be accessible to any function or code in affiliation to the constructor.

this.$func1 = _func1;
this.$name = name;

  1. prototype ???

A proto or prototype means property and in this our program, we will use it to create a property of our constructor which technically means the object of the constructor.

$ContructorFunc.prototype.func1 = function(){}

  1. dondo proto???

dondo proto is another technic we can use to create a property of our constructor but, never use it please it exposes your interval prototype!. Always use a prototype pattern or create the object inside your constructor then you implement it.

$ContructorFunc.prototype.prototype.proto.funcToImplement = function();

6.Lets implement all this now

Alt Text

Alt Text

Alt Text

Alt Text

Finally,i will like to tell you that,i do release new articles every friday by midnight.So, you can always check my blog for new articles:https://you-must-know-javascript.netlify.com/article.html

Also if there is any topic you want us to clarify in javascript,don't hesitate to hit me up in my mail or LinkedIn.

Thanks for reading,

Emmanuel Onah.

Top comments (0)