Index 14 Js Objects and Classes
Index 14 Js Objects and Classes
Javascript objects
A class is defined as the collection of similar types of objects .Therefore also known as blueprint or
template whereas an object is the instance of a class.
In javascript, users can define their own set of objects also known as user-defined objects.
1. By object literal
An object literal is a comma separated list of paired values enclosed in curly braces.
The syntax for creating object literal is
Var object={
property1:value1,
property2:value2…………propertyN:valueN
};
Example Program:
<html>
<head>
<title>js object literal</title>
<h1>Compiled by Er.Gaurab Mishra</h1>
</head>
<body>
<script type="text/javascript">
var car={
make:'Toyota',
model:'RAV4',
year:2022,
number:"NEPAL-2345"
};
document.write(car.make + " " + car.model + " " + car.year +" " + car.number)
</script>
</body>
</html>
<html>
<head>
<title>Access js object properties and methods</title>
<h1>Compiled by Er.Gaurab Mishra</h1>
</head>
<body>
<script type="text/javascript">
var car={
make:'Toyota',
model:'RAV4',
year:2022,
number:"NEPAL-2345",
display:function(){
return this.make +" " + this.model + " " + this.year +" " + this.number;
}
};
document.write(car.display());
</script>
</body>
</html>
Example Program:
<html>
<head>
<title>By creating instance of an object</title>
<h1>Compiled by Er.Gaurab Mishra</h1>
</head>
<body>
<script type="text/javascript">
var car=new Object();
car.make='Toyota';
car.model='RAV4';
car.year=2022;
car.number="NEPAL-2345";
document.write(car.make + " " + car.model
+ " " + car.year +" " + car.number)
</script>
</body>
</html>
Syntax:
Var object1=new functionname(value1,value2….);
Here object1 is an object created with a new keyword and function name is the function used by
the programmer.
We will be using the same name for the function as created with the new keyword.That will be
called as object constructor function.
Example Program:
<html>
<head>
<title>By using an object constructor</title>
<h1>Compiled by Er.Gaurab Mishra</h1>
</head>
<body>
<script type="text/javascript">
function emp()//emp is an object constructor function
{
this.id=12;
this.name='sameer';
this.age=27;
}
var e=new emp();//object created with new
document.write(e.id + " " + e.name
+ " " + e.age)
</script>
</body>
</html>
Function() constructor
IN oop, a constructor is a special type of function which helps to create an object.It creates a new
object with some initial values for respective member variables.
The normal user-defined function is defined by using the keyword function whereas the
constructor is defined using new Function()
Syntax:
Example program
<html>
<head>
<title>js function constructor</title>
<h1>Compiled by Er.Gaurab Mishra</h1>
</head>
<body>
<script type="text/javascript">
var area=new Function("l","b","return l*b");
function display()
{
var result;
result=area(10,20);
document.write(result);
}
</script>
<button type="button" onclick="display()">Click me</button>
</body>
</html>
Array objects
An array can also be defined as an object in js.An array is the collection of similar types of data treated
as a single unit.
Each of the items of the array can be accessed using array index which starts from 0 and ends with
arraysize-1, where size defines the length of an array.
Example program:
<html>
<head>
<title>js array objects</title>
<h1>Compiled by Er.Gaurab Mishra</h1>
</head>
<body>
<script type="text/javascript">
var cars=new Array(5);
cars=['maruti','toyota','bmw','szk','abc'];
document.write(cars);//displays all
document.write("<br>");
document.write(cars.pop() + "<br>");
document.write(cars + "<br>");
document.write(cars.push('xyz') + "<br>");
document.write(cars.reverse() + "<br>");
document.write(cars.sort() + "<br>");
</script>
</body>
</html>
String objects
A string is defined as an array of characters .String automatically inserts a null character ‘\0’ at the end
of the string.
Syntax:
var string_name=new String(string);
Example program
<html>
<head>
<title>js string objects</title>
<h1>Compiled by Er.Gaurab Mishra</h1>
</head>
<body>
<script type="text/javascript">
var str=new String("Hello");
document.write(str.length + "<br>");
document.write(str.toLowerCase() + "<br>");
document.write(str.toUpperCase() + "<br>");
document.write(str.charAt(2) + "<br>");
</script>
</body>
</html>
Math objects
The math object allows you to perform mathematical tasks. The Math object includes several
mathematical constants and methods.
Example Program
<html>
<head>
<title>js Math objects</title>
<h1>Compiled by Er.Gaurab Mishra</h1>
</head>
<body>
<script type="text/javascript">
var pi_value=Math.PI;
var sqrt=Math.sqrt(16);
var round_value=Math.round(2.6);
var random_value=Math.random();
var floor_value=Math.floor(4.7);
var ceil_value=Math.ceil(2.3);
var max_value=Math.max(2,3,6,7,9);
var min_value=Math.min(56,89,4,7,8,2);
document.write(pi_value+"<br>");
document.write(sqrt+"<br>");
document.write(round_value+"<br>");
document.write(random_value+"<br>");
document.write(floor_value+"<br>");
document.write(ceil_value+"<br>");
document.write(max_value+"<br>");
document.write(min_value+"<br>");
</script>
</body>
</html>
Date Objects
You create an instance of the Date object with the "new" keyword.
<html>
<head>
<title>js Date objects</title>
<h1>Compiled by Er.Gaurab Mishra</h1>
</head>
<body>
<script type="text/javascript">
var my_date=new Date();
document.write(my_date + "<br>");
//returns the day of the month(1-31)
document.write(my_date.getDate()+"<br>");
//returns the day of the week(0-6)
document.write(my_date.getDay()+"<br>");
//returns the year(4 digits)
document.write(my_date.getFullYear()+"<br>");
//returns the month ( 0-11)
document.write(my_date.getMonth()+"<br>");
//Returns the hour (0-23)
document.write(my_date.getHours()+"<br>");
//returns milliseconds(0-999)
document.write(my_date.getMilliseconds()+"<br>");
//returns minutes ( 0-59)
document.write(my_date.getMinutes()+"<br>");
//returns seconds (0-59)
document.write(my_date.getSeconds()+"<br>");
</script>
</body>
</html>