0% found this document useful (0 votes)
3 views

Object and Class in Javascript

Uploaded by

Seth Suplico
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Object and Class in Javascript

Uploaded by

Seth Suplico
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

WHAT IS OBJECT IN JAVASCRIPT?

Is the most important datatype in JavaScript. This datatype is


completely different from the primitive datatypes. That means in
the primitive only ONE VALUE is stored but an object can store
MORE THAN ONE VALUE even of different types.

Object Creation
Here is the syntax to declare an object with the name object_name and having the
members inside it having key-value pairs and all the members are enclosed inside { } .

Syntax:

const object_name = {
key_1 : value_1,
key_2 : value_2,

}
JavaScript Object Properties
In JavaScript the members inside the object which are key: values are called
Object properties. For example , in the above – given syntax key_1: value_1 and key_2:
value_2 are the properties of the object.

To Access Object Properties :

1. Using dot Notation:

Syntax:
object_name.key_
1
2.Using bracket Notation:

Syntax:

object_name[“key_
1”]
JavaScript Nested Objects: In this case , an object contains another object inside it.
Syntax:
const object_name-1 = {
key_1 : value_1,
key_2 : value_2,
const object_name-2 = {
key_3: value_3,
key_4: value_4;
}
}

JavaScript Object Methods: In JavaScript, we can add methods to Objects

Syntax:

const object_name-1 = {
method_name: function
(){
//code here
}
}
CLASSES
Classes were first introduced in the new version of the ES6
classes which replaced the previously used functions.
Class is nothing but a blueprint for an object of it. It is used
to create an object mainly. If we relate it to a real-life
example then it is like a plan for a building or house where
that plan contains details about doors, windows, floors,
etc. Based on the class which is the blueprint an object is
made which can be referred to as a house in this example.
So one plan is used to make a lot of houses in the same
way one class can be used to create a lot of classes. So
class is not a real-life entity but the object is one.
THE CONSTRUCTOR METHOD
The constructor method is a special method in JavaScript
with the exact name ‘constructor’. It is automatically
executed when a new object is created from a class. Its
primary purpose is to initialize object properties, allowing
you to define the initial state of an object during its
instantiation.
CREATING JAVASCRIPT CLASSES
To create a JavaScript class, we must follow the
following syntax.

Syntax:
// creating a class
class Name {
constructor(var)
{
this.var =
var;
}
JAVASCRIPT CLASS METHODS
Defining class methods in JavaScript is easy and simple,
we just need to add () following a method name.
class Name {
Syntax:
constructor(var)
{
this.var
= var;
}
// defining
method
method() {
CLASS GETTERS AND SETTERS
We can use getter and setter methods to get the value of an
object and set the value of an object. We can use the get
keyword for the getter method and the set keyword for the
setter methods. class Name {
constructor(var) {
Syntax: this.var = var;
}
// defining getter
method
get method() {
//Code Here
}
// defining setter
method
set method(value) {
this.var = value;
}
Example: The code below demonstrates the creation
and different implementations of JavaScript Classes.
class OOPs { let obj = new OOPs
constructor(name) { (‘JavaScript’);
this.name = name; console.log(obj.name);
}
obj.langName = ‘Java’;
// Getter method console.log(obj.name);
get langName() {
return this.name;
}

// Setter method
set langName(x) {
this.name = x;
}
hello() {
console.log(‘Hello $
{this.name}’)
}
THANK
YOU

You might also like