一个JavaScript的程序

来源:百度知道 编辑:UC知道 时间:2024/05/27 01:35:22
用带参数的构造函数定义一个对象Employee(雇员),它包含两个属性name(名字)和salary(工资)。在Employee函数的prototype上添加一个getSalary方法,用于返回当前对象的salary属性值。在Employee函数的prototype上添加一个addSalary方法,该方法有一个参数addition,用于使当前对象的salary属性增加addition所表示的值。创建三个对象实例boss1、boss2和boss3,他们的名字和工资分别是"Joan", 2000、"Kim", 1000和"Sam", 1500。输出这三个雇员的工资额。将boss1的工资增加200,然后输出boss1的工资额。

<script type="text/javascript">
function Employee(Pname,Psalary){
this.name=Pname;
this.salary=Psalary;
}
Employee.prototype.getSalary=function(){
return this.salary;
}
Employee.prototype.addSalary=function(addition){
this.salary+=addition;
}
var boss1=new Employee("Joan",2000);
var boss2=new Employee("Kim",1000);
var boss3=new Employee("Sam",1500);
document.write("boss1's Salary is "+boss1.getSalary()+"<br/>");
document.write("boss2's Salary is "+boss2.getSalary()+"<br/>");
document.write("boss3's Salary is "+boss3.getSalary()+"<br/>");
boss1.addSalary(200);
document.write("boss1's Salary is "+boss1.getSalary()+" after addition<br/>");
</script>