DEV Community

Raj Madisetti
Raj Madisetti

Posted on

2

The Function of Objects in Javascript

Hello fellow Javascript coders,

This article will provide a clear and succinct explanation of the qualities of objects in Javascript and the various uses that each type of implementation has in creating the most efficient and least memory-intensive functions.

In its very definition, Javascript is a prototypal object-oriented language which means that everything in JS is an object of some type except for the primitive data types (number, string, and boolean). An object can have properties that can be called using dot notation. For example, we can take a look at this object:

var car1 = {
company: 'Ford',
model: 'Mustang',
color: 'red',
isLocked: false
};

The object car1 has three properties company, model, and color and the line car1.model will, in turn, return the value 'Mustang'. Objects can also have methods which are functions that are specific to the object and are not defined above or outside its scope. We can take the same car1 object and add two methods paintBlue() and lock():

var car1 = {
company: 'Ford',
model: 'Mustang',
color: 'red',
isLocked: false,
paintBlue() {
this.color = 'blue';
},
lock() {
this.isLocked = true;
}
};

Now that these methods are added in the car1 object, they can be called using dot notation just like any other property. For example, car1.paintBlue() will change car1.color from 'red' to 'blue'. Note, also, that the this keyword has to be used to allow the object to reference its own property and not change the scope of the function.

With the functionality of properties and methods, objects are very integral in creating helpful Javascript functions and, even, full-stack web applications.

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay