DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
Javascript

JavaScript Objects Overview

Share

JavaScript is an Object Oriented Programming i.e. OOP language. It provides four basic capabilities i.e. Encapsulation, Aggregation, Inheritance and Polymorphism.

For example as a car. It has properties like weight and color and and methods like start and stop. So, all cars have same properties, but the values differ from one car to another.

Object Properties:

Syntax for adding a property to an object:
objectName.objectProperty = propertyValue;

var car = {
type:"Nissan",
model:"F100",
color:"Black"
};

So, the name:values pairs are called properties.

var person = {
firstName:"John",
lastName:"Doe",
age:35
};

Object Methods:

Methods are like actions which can be performed on objects. This is attached to an object and can be referenced by the “this” keyword.

For example:

document.write("Hello Dolly!");

This is write() method of document object to write any content.

How to access:

Using two ways you can access object properties.

1. objectName.propertyName
2. objectName[“propertyName”]

Let’s see another example:

This is how to create an object and assign the value in it.

<script type="text/javascript">
var book = new Object();
book.subject = "JavaScript";
book.author  = "Mr. Dunga";
</script>

And this is how to access the object value.

<script type="text/javascript">
document.write("Name : " + book.subject + "<br>");
document.write("Author : " + book.author + "<br>");
</script>

Output:
Name : JavaScript
Author : Mr. Dunga

Read Also: JavaScript Browsers Handling

Use Methods for an Object:

<script type="text/javascript">
function addPrice(amount){
this.price = amount;
}

function car(name, model){
this.name = name;
this.model  = model;
this.addPrice = addPrice;
}

var myCar = new car("Nissan", "N-100");
myCar.addPrice(45000);

document.write("Name : " + myCar.name + "<br>");
document.write("Model : " + myCar.model + "<br>");
document.write("Price : " + myCar.price + "<br>");
</script>

Output:
Name : Nissan
Model : N-100
Price : 45000

Jacob Frazier

Based on United States, Jacob Frazier is a skilled JavaScript developer with over 8 years of experience. He is passionate about change and trying new things, both professionally and personally. He loves startups and is extremely proactive.

Recent Posts

Why is SEO a non-negotiable part of modern marketing?

In a world where digital presence is paramount, the question isn't whether you should do…

5 hours ago

Innovations in Hair Care: Exploring Breakthroughs for Lasting Results

Over the years, people have experimented with various methods to maintain healthy and beautiful hair.…

1 day ago

How To Strengthen Your Brand-Building Efforts?

Your brand more than developing an attractive and creative logo and infectious motto. It's the…

2 days ago

How To Be Successful And Maximize Compensation In A Personal Injury Case

Introduction Are you someone who has suffered from a personal injury and want to file…

2 days ago

What Are The Biggest Challenges Of Working From Home?

Operating from home has emerged as one of the most popular ways of doing jobs…

3 days ago

Art As An Asset: Will NFT’s Help You Survive Hyperinflation?

If the consequences of our society’s ever-growing debt are what worries you, then it is…

4 days ago