Javascript Objects


Simple Object

<html><head></head> <body> <script>
var objEmployee= { name:'Tim', 
surname:'TimSurname',age:34,salary:1000};
document.write('Name: ',objEmployee.name,"<br>");
document.writeln('Surname: ',objEmployee.surname,"<br>");
document.writeln('Age: ',objEmployee.age,"<br>");
document.writeln('Salary: ',objEmployee.salary,"<br>");
</script></body></html>



Simple Object with Method

<html><head></head> <body> <script>
var objEmployee= {name:'Tim',surname:'TimSurn',age:34,salary:1000,
    show:function(){
    document.write('Name: ',objEmployee.name,"<br>");
document.writeln('Surname: ',objEmployee.surname,"<br>");
document.writeln('Age: ',objEmployee.age,"<br>");
document.writeln('Salary: ',objEmployee.salary,"<br>");}};
objEmployee.show();
</script></body></html>




Alter Properties , Methods
<html><head></head> <body> <script>
var objEmployee ={};
objEmployee.name='Tim';
objEmployee.surname='TimSurname';
objEmployee.age=34;
objEmployee.salary=1000;
objEmployee.show=function() {
document.write('Name: ',objEmployee.name,"<br>");
document.writeln('Surname: ',objEmployee.surname,"<br>");
document.writeln('Age: ',objEmployee.age,"<br>");
document.writeln('Salary: ',objEmployee.salary,"<br>");}
objEmployee.show();
</script></body></html>



Many Get Methods
<html><head> </head> <body> <script>
var objEmployee={name: 'Tim', surname:'TimSur',age:34,salary:1000,
showName:function(){return this.name;}, 
    showSurname:function(){return this.surname;},
    showAge:function(){return this.age;},
    showSalary:function(){return this.salary;} };
document.write("Name: ",objEmployee.showName(),"<br>");
document.write("Surname: ",objEmployee.showSurname(),"<br>");
document.write("Age: ",objEmployee.showAge(),"<br>");
document.write("Salary: ",objEmployee.showSalary(),"<br>");
</script></body></html>




Constructor
<html><head> </head> <body> <script>
function objEmployee(name,surname,age,salary){
this.name=name;
    this.surname=surname;
    this.age=age;
    this.salary=salary;
}
var emp1=new objEmployee('Tim','TimSurname',34,1200);
document.write("Name: ",emp1.name,"<br>");
document.write("Surname: ",emp1.surname,"<br>");
document.write("Age: ",emp1.age,"<br>");
document.write("Salary: ",emp1.salary,"<br>");
</script></body></html>



Constructor with Methods
<html><head> </head> <body> <script>
function objEmployee(name,surname,age,salary){
this.name=name;
    this.surname=surname;
    this.age=age;
    this.salary=salary;
    this.getName=function(){return this.name;};
    this.getSurname=function(){return this.surname;};
    this.getAge=function(){return this.age;};
    this.getSalary=function(){return this.salary;};
}
var emp1=new objEmployee('Tim','TimSurname',34,1200);
document.write("Name: ",emp1.getName(),"<br>");
document.write("Surname: ",emp1.getSurname(),"<br>");
document.write("Age: ",emp1.getAge(),"<br>");
document.write("Salary: ",emp1.getSalary(),"<br>");
</script></body></html>



Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου