JAVA-SCRIPT OBJECT
<!DOCTYPE html>
<html>
<body>
<p>Creating a JavaScript Object.</p>
<p id="demo"></p>
<script>
var employee = {
firstName : "govind",
middleName : "ballabh",
lastName : "khan",
age : 27,
salary : 10000.00,
empId:"0105OIST"
};
//Adding New Properties
employee.nationality="english";
//deleting properties
delete employee.age;
document.getElementById("demo").innerHTML = "Full Name="+
employee.firstName + " "+employee.middleName+" "+employee.lastName+" " +"and salary="+employee.salary+" "+"Nationality="+employee.nationality+" "+ "+Age= "+employee.age
</script>
</body>
</html>
o/p:-----------------------------------------------
Creating a JavaScript Object.
Full Name=govind ballabh khan and salary=10000 Nationality=english +Age= undefined
=======================================================================================================================================================================
<!DOCTYPE html>
<html>
<body>
<p>Creating a JavaScript Object with new keyword</p>
<p id="demo"></p>
<script>
var employee = new Object();
employee.firstName = "govind";
employee.middleName = "ballabh";
employee.lastName = "khan";
employee.age = 27;
employee.salary = 10000.00;
employee.empId ="0105OIST";
var t=employee;//java script objects are mutable
employee.age=26;
document.getElementById("demo").innerHTML = "Full Name="+
employee.firstName +" "+employee.middleName+" "+employee.lastName+" " +"and age="+employee.age;//access the propery of object ist way
/*
document.getElementById("demo").innerHTML = "Full Name="+
employee.firstName +" "+employee.middleName+" "+employee.lastName+" " +"and EmployeeId="+employee["empId"]; ////access the propery of object 2nd way */
</script>
</body>
</html>
o/p:-------------------------------------
Creating a JavaScript Object with new keyword
Full Name=govind ballabh khan and age=26
============================================================================================================================================
<!DOCTYPE html>
<html>
<body>
<p>Creating a JavaScript Object and function</p>
<p id="demo"></p>
<script>
var employee = {
firstName : "govind",
middleName : "ballabh",
lastName : "khan",
age : 27,
salary : 10000.00,
empId:"0105OIST",
fullName : function ()
{
return this.firstName+" "+this.middleName+" "+this.lastName;
}
};
//Adding a Method to an Object
/*employee.name = function() {
return this.firstName + " "+" "+this.middleName+" " + this.lastName;
};*/
document.getElementById("demo").innerHTML = employee.fullName();
/*document.getElementById("demo").innerHTML ="the employee name is "+ employee.name();*/
/*document.getElementById("demo").innerHTML = employee.fullName;//it will return function definition */
</script>
</body>
</html>
o/p:---------------------------------------------------
Creating a JavaScript Object and method
govind ballabh khan
=========================================================================================================================================
<!DOCTYPE html>
<html>
<body>
<p>Creating a JavaScript Object and used for in loop </p>
<p id="demo"></p>
<script>
var employee = new Object();
employee.firstName = "govind";
employee.middleName = "ballabh";
employee.lastName = "khan";
employee.age = 27;
employee.salary = 10000.00;
employee.empId ="0105OIST";
var txt=" ";
var x;
for(x in employee)
{
txt=txt+employee[x]+" "+"<br>";
}
document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
o/p:------------------------------------------------
Creating a JavaScript Object and used for in loop
govind
ballabh
khan
27
10000
0105OIST
==========================================================================================================================================
<!DOCTYPE html>
<html>
<body>
<p>Creating a JavaScript Object with new keyword</p>
<p id="demo"></p>
<script>
var employee = {
firstName : "govind",
middleName : "ballabh",
lastName : "khan",
age : 27,
salary : 10000.00,
empId:"0105OIST",
get employeeId()
{
return this.empId;
//return this.empId.toLowerCase();
}
};
document.getElementById("demo").innerHTML = employee.employeeId;
</script>
</body>
</html>
o/p:---------------------------------------------
Creating a JavaScript Object with get method concept
0105OIST
===========================================================================================================================================
<!DOCTYPE html>
<html>
<body>
<p>Creating a JavaScript Object and setter method</p>
<p id="demo"></p>
<script>
var employee = {
firstName : "govind",
middleName : "ballabh",
lastName : "khan",
age : 27,
salary : 10000.00,
empId:"0105OIST",
set sal(value)
{
this.salary=value;
}
};
employee.sal=11000.00;
document.getElementById("demo").innerHTML = employee.salary;
</script>
</body>
</html>
o/p:-----------------------------------------------------------
Creating a JavaScript Object and setter method
11000
========================================================================================================================================
<!DOCTYPE html>
<html>
<body>
<p>Creating a JavaScript constructor</p>
<p id="demo"></p>
<script>
function employee(first,middle,last,age,sal,id)
{
this.firstName=first;
this.middleName=middle;
this.lastName=last;
this.age=age;
this.salary=sal;
this.empId=id;
}
//create a employee object
var employeeFirst= new employee("govind","ballabh","khan",27,10000.00,"0105OIST");
var employeeSecond= new employee("sanjeet","shankar","jha",25,15000.00,"0106OIST");
document.getElementById("demo").innerHTML = "employeeFirst salary is="+employeeFirst.salary+" employeeSecond salary="+employeeSecond.salary;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p>You cannot add a new property to a constructor function if u want to add then prototype concept used </p>
<p id="demo"></p>
<script>
function employee(first,middle,last,age,sal,id)
{
this.firstName=first;
this.middleName=middle;
this.lastName=last;
this.age=age;
this.salary=sal;
this.empId=id;
}
//create a employee object
//employee.nationality="english";
employee.prototype.nationality = "English";
var employeeFirst= new employee("govind","ballabh","khan",27,10000.00,"0105OIST");
var employeeSecond= new employee("sanjeet","shankar","jha",25,15000.00,"0106OIST");
document.getElementById("demo").innerHTML = "employeeFirst salary is="+employeeFirst.salary+" employeeSecond salary="+employeeSecond.salary+" "+"employee nationality ="+employeeFirst.nationality;
</script>
</body>
</html>
o/p:----------------------
You cannot add a new property to a constructor function if u want to add then prototype concept used
employeeFirst salary is=10000 employeeSecond salary=15000 employee nationality =English
No comments:
Post a Comment