Creating Classes in JavaScript

So you wanna learn how to create Classes in JavaScript. Heck, maybe you wanna know how to do object-oriented stuff in JavaScript! I plan to write an elaborate article on object-oriented JavaScript some day, but for now I will show you how to create a class in JavaScript.

You already must be familiar with JavaScript object instantiation a.k.a creation of a JavaScript objects. Like var d = new Date(), var a = new Array() etc. If you are not familiar with these code, you need to study more of and play around with JavaScript.

Do you wanna create your own JavaScript classes and do stuff like var m = new Monkey()? Creating a class called Monkey and instantiating Monkey objects. It is very simple to create classes in JavaScript - just define a well-named function, and you have your own class!

In the example below, we create a Monkey class, instantiate it, and then access its variables and methods.

All objects defined inside the function are properties of the class. Those attached to this are public properties and those defined with a var are private properties. Properties which are assigned to functions become the class methods and those assigned to variables become class variables. Savvy?

I am gonna be doing a very detailed article on Object-oriented JavaScript (OOJS) one of these days, more information awaits you there - in the future.

TLDR: Every function in JavaScript is a class, just instantiate it like how you would do for any other class. Eg: var f = new MyGoddamnFunction(). Make sure you have defined the function first and have created the necessary variables and functions in the class function.