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

Recent Posts

Guest Topic Name : Top 5 Must-Have Features Every Mobile App Needs to Succeed

Introduction Businesses understand the diverse requirements of mobile applications, which provide a competitive advantage. There…

2 hours ago

IoT Data Analytics: Ways to Gain Value from IoT Data

The Internet of Things (IoT) has recently changed the world. It links gadgets together and…

3 hours ago

The Rise of NFTs: Exploring the Impact of Non-Fungible Tokens on the Digital Economy

NFTs, or Non-Fungible Tokens, are revolutionizing the digital economy. These unique digital assets, authenticated through…

20 hours ago

Unveiling the Truth: Is the Spread of Sinus Infections a Myth or Reality?

Sinus infections, impacting approximately 31 million Americans each year, represent a significant health concern stemming…

2 days ago

Best Exercises To Reduce Weight & Keep You Stronger & Fitter!

No doubt that balanced weight is the key to wellness. So, when it comes to…

2 days ago

ARTIFICIAL INTELLIGENCE: Advantages And Disadvantages? Everything You Need to Know

Pros And Cons Of AI: Artificial Intelligence directly translates to conceptualizing and building machines that…

2 days ago