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)