Unit 3_oops and Dom
Unit 3_oops and Dom
1. By object literal
An object literal is a list of name:value pairs inside curly braces {}.
Syntax: Object={property1:value, pro2:value,……}
Example:
<!DOCTYPE html>
<html>
<body>
<h1>Creating JavaScript Objects</h1>
<h2>Using an Object Literal</h2>
<p id="demo"></p>
<script>
// Create an Object:
const person = {firstName:"RAM", lastName:"Das", age:20, eyeColor:"black"};
<p id="demo"></p>
<script>
// Create an Object:
let person= new Object()
person.fname=”RAM”;
person.age=20;
Some method:
getDate()- return day of the month for the specified date according to local
time.
getDay()-return day of the week for the specified date according to local time.
etc.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p>Create a new date object with the current date and time:</p>
<p id="demo"></p>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d;
const d1 = new Date("October 13, 2014 11:13:00");
document.getElementById("demo1").innerHTML = d1;
</body>
</html>
2. Number Object:
The Number object represents numerical date, either integers or floating-
point numbers.
Syntax: let val=new Number(number);
3. String Object:
The string object is used to work with a series of characters.
Syntax: let val= new String(string);
There are 2 ways to create sting in javascript
1. By string literal
Syntax
let str= “string here”
2. By sting object (new keyword)
Syntax:
let str= new String(“string here”);
some methods are:
concat()- combine two text
slice()- Extract a section of a string
spit()- splits a string object into an array of string by separating the string
into substing.
toUpperCase()- converted into Upper case
toLowerCase()- convert Upper into lower case.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The slice() Method</h2>
<p>The sliced (extracted) part of the string is:</p>
<p id="demo"></p>
<script>
let text = "Apple, Banana, Kiwi";
let part = text.slice(7,13);
document.getElementById("demo").innerHTML = part;
</script>
</body>
</html>
4. Array Object:
JavaScript array is an object that represents a collection of similar type of elements.
There are 3 ways to construct array in JavaScript
1. By array literal
2. By creating instance of Array directly (using new keyword)
3. By using an Array constructor (using new keyword)
1) JavaScript array literal
Syntax
var arrayname=[value1,value2.....valueN];
example:
<script>
var emp=["Sonoo","Vimal","Ratan"];
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br/>");
}
</script>
Test it Now
The .length property returns the length of an array.
2) JavaScript Array directly (new keyword)
The syntax of creating array directly is given below:
var arrayname=new Array();
Here, new keyword is used to create instance of array.
<script>
var i;
var emp = new Array();
emp[0] = "Arun";
emp[1] = "Varun";
emp[2] = "John";
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br>");
}
</script>
Tes
Output of the above example
Arun
Varun
John
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math Constants</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"<p><b>Math.E:</b> " + Math.E + "</p>" +
"<p><b>Math.PI:</b> " + Math.PI + "</p>" +
"<p><b>Math.SQRT2:</b> " + Math.SQRT2 + "</p>" +
"<p><b>Math.SQRT1_2:</b> " + Math.SQRT1_2 + "</p>" +
"<p><b>Math.LN2:</b> " + Math.LN2 + "</p>" +
"<p><b>Math.LN10:</b> " + Math.LN10 + "</p>" +
"<p><b>Math.LOG2E:</b> " + Math.LOG2E + "</p>" +
"<p><b>Math.Log10E:</b> " + Math.LOG10E + "</p>";
</script>
</body>
</html>
Event Handling
JavaScript’s interaction with HTML elements is handled through events that occur when the
user or the browser manipulates a page. When the page loads, it is called an event.
Examples include events like pressing any key, closing window, or resizing a window.
Event handlers are the code that invokes a specific piece of code when a particular action
happens on an HTML elements.
It embeds an image or video clip into the current document. The img tag allows the
insertion of .jpg, .png, .bmp abd .gif image files into HTML document. In many web
application, it is important to know whether a specific image has been loaded, or in the
process of loading or the loading process is interrupted. The image event handling provides
this capability.
A form provides different GUI (Graphical User Interface) objects or elements that are used
to send and receive information to and from the webserver. These objects include buttons,
text boxes, checkboxes, radio buttons, etc. each object is associated with a number of
events.
<html>
<head>
</head>
<<body>
<h1>Testing Form Event Handling </h1>
<form onSubmit='alert("You are submitting data");'>
Enter your name: <input type="text" >
<input type="submit" value ="submit data">
</form>
<body>
</html>