Skip to content

JavaScript Constructor Functions

In the real world construction workers are everywhere. They build our houses, commercial buildings, roads. We really can’t live without them. In programming constructor functions are very similar. They are your virtual construction workers, and they build objects. They can save you a lot of time. It’s beneficial to save time, but more importantly: in JavaScript, using constructor functions allows you to properly attach a prototype object. Prototypes allow you to create objects with similar properties, consistently and efficiently. For instance if you are working on a game you might need to create a few hundred enemies. You’re definitely not going to want to copy and paste all the code to create your variable each time SO… You need CONSTRUCTORS.

Let’s make a game. It’s going to have a lot of Ogres.

Let’s construct a typical Ogre.

So, now we’ve got our prototype for an average Ogre. We can create a new Ogre that will be it’s own JavaScript object. Let’s make three identical ogres- because we’re going to need lots of enemies for the game.

So now we have three variables: ogre1, ogre2, and ogre3 and they each have their own properties set for strength, health, defense, and attack based on our constructor function.

 

I’m sure you can already see why this is such a handy thing, but let’s expand on it. We need to make a few tougher-to-kill Ogres. We will call them captain ogres. This time we will make a new constructor function that allows us to pass parameters in for strength, health, defense, and attack.

Now we can specify the parameters of our captain ogres each time that we build one. We want these enemies each to be slightly different to make the player feel like they are battling enemies that are unique.

Now we’ve stored a variable capOgre1 for our 1st captain Ogre.

It has 400 strength, 2000 health, 400 defense, and 800 attack.

Our second captain ogre has 500 strength, 1800 health, 350 defense, and 600 attack.

 

For our third captain ogre we’re going to make a boss. He’s still going to be a captain ogre, but we’re going to make him much harder to beat. He will look like the other captain ogres but unexpectedly to the player- he will be much harder to eliminate.

bossOgre has 1000 strength, 9000 health, 800 defense, and 1200 attack.

 

I could go on and on with this, and that’s the idea. Prototyping is a great way to save yourself a whole lot of keystrokes when you need to create a multitude of similar JavaScript objects. You can target the properties of each of these unique objects inside your functions too.

 

As a simplified example- where e is the enemy and p is the players power of attack- an attack function might be defined like this:

We’ve defined our function, now let’s call this function when the player attacks ogre1.

We’ll pass in the enemy and the attack power of 100.

This function would effectively reduce the ogres health by 100.
Obviously these examples don’t take into account the defense levels of each ogre or the strength of the player, but it is possible to work that into your functions as well. At this point It should be pretty obvious to you just how much time JavaScript constructor functions can save. Use those virtual construction workers, they are your friends.