Javascript Objects

In this code article we are going to take a clearer look into what JavaScript objects are.

What is an Object?:

An object is anything which can be touched and seen, which means that it has a body and can be seen. Examples, person, car, plane, house, television, etc. As we can see from the examples as listed, we can agree that for anything to be considered as an object, it then means that it has individual properties that qualifies it to be an object such as color, weight, model, age, etc.

Creating Objects in JavaScript:

There are three(3) ways to create an Object in JavaScript.

  1. By object literal
  2. By creating instance of Object directly (using new keyword)
  3. By using an object constructor (using new keyword)

By object literal:

The word 'literal' can be said to mean taking words in their usual or most basic sense without metaphor or exaggeration according to Oxford languages and google. By this we are simply saying that a particular object has a particular property which has a particular value.

Object literal syntax;

Object = {property1: value1, propert2: value2, propertyN: valueN};

Let's look at an examples below;

car = {brand: "mercedes", model: "maybeach", year:2022, color: "black"};
console.log(car.brand+", "+car.model+", "+car.year+", "+car.color)

By creating instances of Object directly (using both new and Object keyword):

From the name description, you can easily figure out what this is talking about. I want to be able to make you understand in simple terms what happens in JavaScript. An example or single occurrence of something can be said to be an instance of a thing.

Instances of an Object syntax;

var objectname = new Object();

Let's look at an examples below;

var car = new Object(); // the word "Object" must start with capital 'O'
car.color = "red";
car.model = "tacoma";
car.year = 2022;
console.log(car.color+", "+car.model+", "+car.year);

By using an object constructor:

As the name implies, it means you are trying to construct your own object(let's say car), but for you to be able to do so, you need something to serve as a guide to you and that thing can be called a prototype. Meaning that for each object you would create it must represent the prototype you are working with. Here, you need to create function(the constructor) with arguments. Each argument value can be assigned in the current object by using 'this' keyword. A constructor is a class or function that specifies the type of the object instance. The this keyword refers to the current object(the prototype).

The example of creating object by object constructor is given below;

var car = function(color, model, year){
this.color = color;
this.model = model;
this.year = year;
}
var car1 = new car("red", "tundra", 2021)
console.log(car.color+", "+car.model+", "+car.year);
//Suppose you define an object called Person as follows
function Person(name, age, sex) {
  this.name = name;
  this.age = age;
  this.sex = sex;
}
//And then instantiate two new Person objects as follows:

var orji = new Person('Afoma Orji', 24, 'F');
var sam = new Person('Samuel Bright', 42, 'M');
console.log(orji.name)
//Then you can also rewrite the definition of Car to include an owner property that takes a Person object, as follows:

function Car(make, model, year, owner) {
  this.make = make;
  this.model = model;
  this.year = year;
  this.owner = owner;
}
//To instantiate the new objects, you then use the following:

var car1 = new Car('Toyota', 'Tacoma', 2020, 'Afoma');
var car2 = new Car('Benz', 'c300', 2022, 'Vick');

console.log(car2.owner);

Okay, now I assume you have understood the three(3) basic ways of creating objects in javascript. Since you have, let us also look at what is called Object methods.

Object method:

What is an object method? To understand this, we will first of all understand what a method is. A method can be described as a procedure for doing somethin, in other words, an action performed by an object.

Defining method in JavaScript Object

We can define method in JavaScript object. But before defining method, we need to add respective properties in the function with same name as method.

The example of defining method in object is given below.

var car =function(make, model, year, owner, speed) {
this.make = make;
this.model = model;
this.year = year;
this.owner = owner;
this.speed = speed;  //speed here is a method

this.changeSpeed = changeSpeed;
function changeSpeed(recentSpeed){
this.speed = recentSpeed;
}
}
car1 = new car('Toyota', 'Tacoma', 2020, 'Afoma', '190km/hr')
console.log(car1.speed); //initial speed
car1.changeSpeed('230km/hr')
console.log(car1.speed); //new speed

I hope you I have made the whole concept of creating an object and object method simplified to you? As always, feel free to leave your comment or ask your question below in the comment section. Thank you!