jecommera
asked on
please look at my code and advise if I could do this better (extending objects)
The question is:
Using JavaScript prototyping, extend the Employee (Exercise 4). Create two
new subclasses from the Employee: a Salesman and a Manager class. Use a prototype
to create a salary property for the new employees (setSalary, getSalary),
and a setRaise() method to add 15 percent to the salary. Check to see if each of
the new Employees is an instance of the Employee class.
<script type="text/javascript">
function Employee(){
var = name;
this.getName = function(){
return this.name;
}
this.setName = function(name){
this.name = name;
};
}
My answer is:
<html>
<head><title>Object object</title>
<script type="text/javascript">
function Employee(){
var name;
this.getName = function(){
return this.name;
}
this.setName = function(name){
this.name = name;
}
}
function Salesman() {}
Salesman.prototype = new Employee();
Salesman.prototype.constru ctor = Salesman;
Salesman.prototype.setSala ry = function(salary) {
this.salary = salary;
}
Salesman.prototype.getSala ry = function() {
return this.salary;
}
Salesman.prototype.setRais e = function() {
return (this.salary+(this.salary* 0.15));
}
function Manager() {}
Manager.prototype = new Employee();
Manager.prototype.construc tor = Salesman;
Manager.prototype.setSalar y = function(salary) {
this.salary = salary;
}
Manager.prototype.getSalar y = function() {
return this.salary;
}
Manager.prototype.setRaise = function() {
return (this.salary+(this.salary* 0.15));
}
</script>
</head>
<body bgColor="#EOFFFF">
<script type="text/javascript">
sm1 = new Salesman();
sm1.setSalary(10000);
alert("The raise will be "+sm1.setRaise());
mr1 = new Manager();
mr1.setSalary(134320);
alert("The raise will be "+mr1.setRaise());
if (mr1 instanceof Manager)
{
alert('correct');
}
if (mr1 instanceof Salesman)
{
alert('correct');
}
else alert('false');
</script>
</body>
</html>
which works fine - I am just wondering if there is a better (neater and shorter) way I could write this code.
thanks
Using JavaScript prototyping, extend the Employee (Exercise 4). Create two
new subclasses from the Employee: a Salesman and a Manager class. Use a prototype
to create a salary property for the new employees (setSalary, getSalary),
and a setRaise() method to add 15 percent to the salary. Check to see if each of
the new Employees is an instance of the Employee class.
<script type="text/javascript">
function Employee(){
var = name;
this.getName = function(){
return this.name;
}
this.setName = function(name){
this.name = name;
};
}
My answer is:
<html>
<head><title>Object object</title>
<script type="text/javascript">
function Employee(){
var name;
this.getName = function(){
return this.name;
}
this.setName = function(name){
this.name = name;
}
}
function Salesman() {}
Salesman.prototype = new Employee();
Salesman.prototype.constru
Salesman.prototype.setSala
this.salary = salary;
}
Salesman.prototype.getSala
return this.salary;
}
Salesman.prototype.setRais
return (this.salary+(this.salary*
}
function Manager() {}
Manager.prototype = new Employee();
Manager.prototype.construc
Manager.prototype.setSalar
this.salary = salary;
}
Manager.prototype.getSalar
return this.salary;
}
Manager.prototype.setRaise
return (this.salary+(this.salary*
}
</script>
</head>
<body bgColor="#EOFFFF">
<script type="text/javascript">
sm1 = new Salesman();
sm1.setSalary(10000);
alert("The raise will be "+sm1.setRaise());
mr1 = new Manager();
mr1.setSalary(134320);
alert("The raise will be "+mr1.setRaise());
if (mr1 instanceof Manager)
{
alert('correct');
}
if (mr1 instanceof Salesman)
{
alert('correct');
}
else alert('false');
</script>
</body>
</html>
which works fine - I am just wondering if there is a better (neater and shorter) way I could write this code.
thanks
Given that it works for you, only way I could think of is to put each class in a different file and include those files in your header tag
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.