Objects in JavaScript? 3 ways to do it.

JavaScript is not an OOP language.  This is one of the challenges faced by server-side programmers, specially if you use OOP languages likes C, C++, C# or Java.  We know that using objects give us clarity and a way to translate the problems we are trying to resolve through code. So, how we can have some of the advantage of use objects in JavaScript, since this is not an OOP Language?

There are 3 ways to create objects in JavaScript: by literal notation, by using the object keyword or by creating a function.

Creating objects by literal notation

Creating an object in JavaScript using a literal notation is very simple. The only caveat for using this type form is that only allows you to create one single object per the whole code. You will need to recreate every time the object if you want to reuse it.  Example:

var animal = {
name: 'Spider',
legs: 8,
eyes: 16,
eat: function(){
console.log('animal ' + this.name + ' is eating.';
}
};

Creating objects by using object keyword

You can create objects using the object keyword, but when creating it you only create a black object.  Properties and methods of the object can only be added outside of the creation code.  This form of creating an object is a little be strange for those who are used to create objects and properties at the same time.  Also, this kind of object creation suffers the same problem as using literal notation: you create the object, but you cannot reuse multiple times.  Example:

var animal = new Object();

animal.name = 'Spider';
animal.legs = 8;
animal.eyes = 16;
animal.eat = function(){
console.log('animal ' + this.name + ' is eating.';
}

Creating object by using functions

If you want to create objects in JavaScript as similar as those you create in some OOP languages, you must consider creating them using functions.  Using functions to create objects allows you to create a template for the object.  This template is the function you are writing to build your object.  This is similar as classes in Java and C#.  It will also allows you to have a pseudo constructor, since creating from a function gives you the ability to use parameters that can act as constructor parameters.  At lastly, it will allow you to reuse the code to create as many objects you want.  Example:

function animal(name, legs, eyes){
this.name = name;
this.legs = legs;
this.eyes = eyes;
this.eat = function(){
console.log('animal ' + this.name + ' is eating.';
}
};

var spider = new animal('Spider', 8, 16);
var dog = new animal('Dog', 4, 2);

Conclusion

As you can see, if you are a server-side developer entering in the JavaScript world, while the language is not an OOP language, you can some part of its syntax to create OOP code.  I will recommend you to create objects in JavaScript using functions since you can have more control in your code.  Also it will allow you to move to more in-depth JavaScript frameworks that use this kind of paradigm.

Rodnney

I am a "multi hat" software developer with more than 18 years in experience. I had worked from government agencies, insurance companies, ticketing system and educational business using a lot of technologies like SQL Server, Oracle, ASP.NET, MVC, HTML, CSS, Javascript, Linq, etc.

You may also like...