Professional Documents
Culture Documents
Unit - 4
Unit - 4
JavaScript is template based not class based. Here, we don't create class to get the object. But, we direct create objects.
1. By object literal
2. By creating instance of Object directly (using new keyword)
3. By using an object constructor (using new keyword)
object={property1:value1,property2:value2.....propertyN:valueN}
<script>
emp={id:102,name:"Shyam Kumar",salary:40000}
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
Page 1
Rofel BBA and BCA College-Vapi
<script>
var emp=new Object();
emp.id=101;
emp.name="Ravi Malik";
emp.salary=50000;
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
<script>
function emp(id,name,salary)
{
this.id=id;
this.name=name;
this.salary=salary;
}
e=new emp(103,"Vimal Jaiswal",30000);
Page 2
Rofel BBA and BCA College-Vapi
<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>
Page 3
Rofel BBA and BCA College-Vapi
4.2:Date Object
4.2.1:Date Constructure
Date Object
The Date object is used to work with dates and times.
1.Date():
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
2. Date(milliseconds):
<html>
<body>
<p>Using new Date(milliseconds), creates a new date object as January 1, 1970, 00:00:00 Universal Time (UTC) plus the
milliseconds:</p>
<p id="demo"></p>
Page 4
Rofel BBA and BCA College-Vapi
<script>
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
3.Date(dateString):
<html>
<body>
<p>A Date object can be created with a specified date and time:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
<body>
<p>Using new Date(7 numbers), creates a new date object with the specified date and time:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
Page 5
Rofel BBA and BCA College-Vapi
4.2.2:Date Methods
1.getDate():
Javascript date getDate() method returns the day of the month for the specified date according to
local time. The value returned by getDate() is an integer between 1 and 31.
e.g
<html>
<head>
</script>
</head>
</html>
Output:
25
2. getDay():
Javascript date getDay() method returns the day of the week for the specified date according to local
time. The value returned by getDay() is an integer corresponding to the day of the week: 0 for Sunday, 1
for Monday, 2 for Tuesday, and so on.
e.g
<html>
<head>
Page 6
Rofel BBA and BCA College-Vapi
</script>
</head>
</html>
Output:
3. getMonth():
Javascript date getMonth() method returns the month in the specified date according to local time. The
value returned by getMonth() is an integer between 0 and 11. 0 corresponds to January, 1 to February,
and so on.
e.g
<html>
<head>
</script>
</head>
</html>
Output:
11
Page 7
Rofel BBA and BCA College-Vapi
4. getHours() :
Javascript Date getHours() method returns the hour in the specified date according to local time. The
value returned by getHours() is an integer between 0 and 23.
e.g
<html>
<head>
</script>
</head>
</html>
Output:
23
5.setDate():
Javascript date setDate() method sets the day of the month for a specified date according to local time.
E.g
<html>
<head>
Page 8
Rofel BBA and BCA College-Vapi
dt.setDate( 24 );
document.write( dt );
</script>
</head>
</html>
Output:
6. setMonth() :
Javascript date setMonth() method sets the month for a specified date according to local time.
E.g
<html>
<head>
dt.setMonth( 2 );
document.write( dt );
</script>
</head>
</html>
Output:
Page 9
Rofel BBA and BCA College-Vapi
7.setDay():
The JavaScript date setDay() method sets the particular day of the week on the basis of local time.
<html>
<Head>
<script>
var date =new Date("May 11, 2019 21:00:10");
document.writeln(date.getDate() + "<br>");
date.setDate(23);
document.writeln(date.getDate());
</script>
</head>
</html>
Output:
11
23
8.toString():
<head>
<title>JavaScript toString Method</title>
<script type = "text/javascript">
var dateobject = new Date(1993, 6, 28, 14, 39, 7);
stringobj = dateobject.toString();
document.write( "String Object : " + stringobj );
</script>
</head>
</html>
Output:
String Object : Wed Jul 28 1993 14:39:07 GMT+0530 (India Standard Time)
Page 10
Rofel BBA and BCA College-Vapi
When html document is loaded in the browser, it becomes a document object. It is the root element that
represents the html document. It has properties and methods. By the help of document object, we can
add dynamic content to our web page.
According to W3C - "The W3C Document Object Model (DOM) is a platform and language-neutral interface
that allows programs and scripts to dynamically access and update the content, structure, and style of a
document."
Page 11
Rofel BBA and BCA College-Vapi
Method Description
getElementsByName() returns all the elements having the given name value.
Page 12
Rofel BBA and BCA College-Vapi
Here, document is the root element that represents the html document.
value is the property, that returns the value of the input text.
Let's see the simple example of document object that prints name with welcome message.
<script type="text/javascript">
function printvalue()
{
var name=document.form1.name.value;
document.write("Welcome: "+name);
document.writeln("Welcome: "+name);
}
</script>
<form name="form1">
Enter Name:<input type="text" name="name"/>
<input type="button" onclick="printvalue()" value="print name"/>
</form>
document.getElementById() method
The document.getElementById() method returns the element of specified id.
In the previous page, we have used document.form1.name.value to get the value of the input value.
Instead of this, we can use document.getElementById() method to get value of the input text. But we need
to define id for the input field.
Let's see the simple example of document.getElementById() method that prints cube of the given
number.
Page 13
Rofel BBA and BCA College-Vapi
<script type="text/javascript">
function getcube(){
var number=document.getElementById("number").value;
alert(number*number*number);
}
</script>
<form>
Enter No:<input type="text" id="number" name="number"/><br/>
<input type="button" value="cube" onclick="getcube()"/>
</form>
document.getElementsByName() method
<script type="text/javascript">
function totalelements()
{
var allgenders=document.getElementsByName("gender");
alert("Total Genders:"+allgenders.length);
}
</script>
<form>
Male:<input type="radio" name="gender" value="male">
Female:<input type="radio" name="gender" value="female">
Page 14
Rofel BBA and BCA College-Vapi
Page 15