JavaScript Objects

by Monika Dadool 22-Jun-19

0 179


JavaScript is an object-based scripting language, in which an object is a non-primitive data type. To work efficiently with JavaScript, knowledge of objects is mandatory.

Objects are a collection of multiple values. These objects are created to solve various object oriented paradigms. There are numerous methods of creating an object in JavaScript. In this article; we will discuss all concepts related to objects, constructors in JS that are different as encountered in traditional languages such as Java and C++. Prototypes in JavaScript, lets you build an object based on a particular template. In JavaScript, there are some shortcuts that let you to build something that resembles classes but in code, there are no concepts of classes found in JavaScript as in other languages. However, a prototype in JavaScript's lets you build the concepts like classes.


Example of an object in real-life
Vehicle is an object
Vehicle comprises of certain properties such as color, methods, brand, and weight.

Properties
vehicle.name= car
vehicle.model=alto
vehicle.color=black

Methods
vehicle.begin()
vehicle.end()
vehicle.execute()

# All vehicles have identical properties but values differ from vehicle to vehicle.
# All vehicles have same methods but each method is performed at different times.
# Object acts like a container for named values known as properties or methods.
# The values are written as name: value pairs i.e. both value and name are separated through a colon.
# Objects are variables also but they contain multiple values.


Let's differentiate between Objects and Varialbles

Objects
Variables
It can contain many values
It contains only single value
Example: var vehicle="car";
var vehicle = {type:"car", color:"red"};

Object properties
The name:value pairs are known as properties in JavaScript.

Example:

var employee={
 firstName:"VIHAAN",
 lastName:"KHANNA"
 salary:22000,
};

Where, first name is property and VIHAAN is the property value


Objects methods
Methods are known as the actions that can be performed on objects. These methods are stored in properties as function definitions.

How to access object properties
You can access object properties by two methods.
i.e. objectName.propertyName or objectName{"propertyName"}


How to create an object in JavaScript:
An object can be created in JavaScript with the help of figure brackets {...} containing optional list of properties. To understand this concept briefly, let us look at an example of a JavaScript Object containing value pairs.

var employee={
 firstName:"VIHAAN",            
 lastName:"KHAN"
 salary:22000,
};

A simple example demonstrating an object in JavaScript

<script>
let employee = {
name: 'VIHAAN',
location : 'USA',
DOB : '1991',
displayInfo : function(){
console.log(`${employee.name} was born
in ${employee.location} in ${employee.DOB}`);
}
}
employee.displayInfo();
</script>


Share:


Discount Coupons

SEMRush FREE Trial – PRO Account for 14 Days

FREE Pro Account worth $99.95 for 14 Days.



Comments

    No Comments Yet.

Leave a Reply