Javascript Prototypes


Length, Constructor

<!DOCTYPE html>
<html> <body> <script>
function af1(a,b){return a+b};
document.write("af1.length: ",af1.length,"<br>");
document.write("af1.constructor:",af1.constructor,"<br>");
document.write("af1.prototype:",af1.prototype,"<br>");
</script></body></html>



Prototype.
<!DOCTYPE html>
<html> <body> <script>
function Employee(name,surname){
this.name=name;
    this.surname=surname;
};
Employee.prototype.age=34;
Employee.prototype.salary=1200;
Employee.prototype.getName=function(){return this.name;};
Employee.prototype.getSurname=function(){return this.surname;};
Employee.prototype.getAge=function(){return this.age;};
Employee.prototype.getSalary=function(){return this.salary;};

function EmployeeW(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;};
};
function takeObject(obj){
document.write(obj.getName(),"<br>");
document.write(obj.getSurname(),"<br>");
document.write(obj.getAge(),"<br>");
document.write(obj.getSalary(),"<br>");
};
var newEmp1=new Employee('Tim','Tim_Sur',22,2345);
takeObject(newEmp1);
var newEmp2=new EmployeeW('Tom','Tom_Sur',44,23451);
takeObject(newEmp2);
</script></body></html>




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

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