They are an extension of actual real world objects.

Objects are used for recreating and using real world objects in JS. You should use objects when dealing with data related to people, things, and so on.

The best example of this i got from the book Beginning JavaScript by Jeremy McPeak and Paul Wilton. They have used the example of a car to clearly define an object.

For instance, let’s say that there is a car object, Bugatti Veyron. This object has some properties and methods.

Properties of Bugatti Veyron:

Color: white with black stripes
Name: Bugatti Veyron
Engine: 7993cc
Methods of Bugatti Veyron

Breaks: Reduce speed when pressed
Accelerator: Increases speed of the car
Hope you were able to capture the essence of what constitutes an object. Properties tell us more about an object and methods alter the state or properties of the object.

So essentially, methods are functions of a particular object.

Let’s look at this through an example:

To create a Bugatti Veyron in JavaScript, type in the following code:

No need to get scared with this syntax. This is known as the literal syntax for creating objects in JS. It is preferred in most situations. Everything in JS is essentially an object. Hence,

We use the var keyword to initialize our variable, bugattiVeyron.
We use the curly braces {} to initialize the object.
We add properties to the object by specifying Key: Value.
How to access Properties of an Object?
To access the property, use the following syntax 😀

How to create and access methods of an object:
To create an object, follow the syntax given below:

In this example we introduced two methods:

accelerator: Increases the speed of car
break: Decreases the speed of car
To call a method, we use:

Everytime you call the accelerator method and pass in a positive number, the speed will be increased. Inverse holds true for the break method. This example delineates how objects work and how we can create a simple object with properties and methods in JS.

Methods are always followed by parentheses (), even if you don’t want to specify any parameters for it.

Play around with these concepts!

Next up, In Detail — How to create objects in JavaScript?

Hope this helped!

Categories: Uncategorized

Weird Programmer

I hate people who use LOL in face to face conversations. Also, if I tell you anything else about myself, then I'll have to KILL you~~!

Leave a Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Notify of
%d bloggers like this: