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

4

notes

Uploaded by

2k23.mca2313345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

4

notes

Uploaded by

2k23.mca2313345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

JAVASCRIPT

1
JavaScript Object
• JavaScript Objects:

❑ Basic run time entity that


have state(Attribute) and behavior(method).
❑ State is a instance variable.
❑ Behavior is a method(function) .

❑ JavaScript is an object-based language. Everything is an object in


JavaScript.
❑ JavaScript is template based not class based. Here, we don't
create class to get the object. But, we direct create objects.

2
JavaScript Object
Creating Objects in JavaScript
• The most common Object Creation ways are as follow.
1. By object literal
2. By creating instance of Object directly (using new
keyword)
3. By using an object constructor (using new keyword)
4. By using class and Constructor Method.

3
JavaScript Object
• 1) JavaScript Object by object literal
Using object literals to create objects in JavaScript involves defining an object
directly with key-value pairs inside curly braces {}. This method is concise and
straightforward, allowing you to quickly create objects with properties and
methods, enhancing code readability.

The syntax of creating object using object literal is given below:

object={property1:value1,property2:value2.....propertyN:valueN}

As you can see, property and value is separated by : (colon).

Let’s see the simple example of creating object in JavaScript.

<script>
emp={id:102,name:"Shyam Kumar",salary:40000}
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
4
JavaScript Object
2) By creating instance of Object

• Object class is super class of all type of classes .you can create object using Object class
directly .

• The syntax of creating object directly is given below:


• var objectname=new Object();

• Here, new keyword is used to create object.


• Let’s see the example of creating object directly.
<script>
var emp=new Object();
emp.id=101;
emp.name="Ravi Malik";
emp.salary=50000;
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>

5
JavaScript Object
3) By using an Object constructor
• Constructors in JavaScript, like in most other OOP languages, provides a template for
creation of objects. In other words, it defines a set of properties and methods that would be
common to all objects initialized using the constructor.
• Here, you need to create function with arguments. Each argument value can be assigned in
the current object by using this keyword.
• The this keyword refers to the current object.
• The example of creating object by object constructor is given below.

<script>
function emp(id,name,salary){
this.id=id;
this.name=name;
this.salary=salary;
}
e=new emp(103,"Vimal Jaiswal",30000);

document.write(e.id+" "+e.name+" "+e.salary);


</script>

6
JavaScript Object
4.By using class and Constructor Method.

•JavaScript Classes are basically a blueprint or template of the object. JavaScript classes can be used to
create new objects in Javascript.
•Syntax:

class classname
{
constructor(parameter)
{
this.instancevariable = parameter;
}}

•The constructor() method is a special method for creating and initializing objects created within a class.
•The constructor() method is called automatically when a class is initiated, and it has to have the exact
name "constructor", in fact, if you do not have a constructor method, JavaScript will add an invisible
and empty constructor method.
7
<html>
<head>
<script>
class emp {
constructor(name, age) {
this.name = name;
this.age = age;

}
}
const emp1 = new emp("A", "25 years");
const emp2 = new emp("B", "23 years");
const emp3 = new emp("C", "21 years");
document.write("Object 1 ")
document.write("Name ",emp1.name,"Age "," ",emp1.age," ","<br>");
document.write("Object 2 ,")
document.write("Name ",emp2.name,"Age ",emp2.age,"<br>");
document.write("Object 3 ,")
document.write("Name ",emp3.name,"Age ",emp3.age);

</script>

</head> 8
<body></body></html>
Write a java Script to find Area of Square and Area of Triangle
using class and Object
<html><body>
<h1>JavaScript Class Methods</h1>
<p>How to define and use a Class method.</p>
<script>
class Area {
constructor(base, height,side) {
this.base = base;
this.height = height;
this.side=side; }
cal_area_triangle()
{
var area1=0.5*this.base*this.height;
document.write("Area of Triangle is ",area1) }
cal_area_square()
{
var area2=this.side*this.side;
document.write("Area of Square is ",area2);}}
const ar1 = new Area(4,6,3);
ar1.cal_area_square();
document.write("<br>")
9
ar1.cal_area_triangle(); </script></body></html>
JavaScript Object
Defining method in JavaScript Object
• We can define method in JavaScript object. But before
defining method, we need to add property in the
function with same name as method.

• The example of defining method in object is given


below.

10
JavaScript Object
<script>
function emp(id,name,salary){
this.id=id;
this.name=name;
this.salary=salary;
this.changeSalary=changeSalary;
function changeSalary(otherSalary){
this.salary=otherSalary;
}
}
e=new emp(103,"Sonoo Jaiswal",30000);
document.write(e.id+" "+e.name+" "+e.salary);
e.changeSalary(45000);
document.write("<br>"+e.id+" "+e.name+" "+e.salary);
</script>

11

You might also like