Solved

please look at my code and advise if I could do this better (extending objects)

Posted on 2012-03-18
2
420 Views
Last Modified: 2012-04-07
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.constructor = Salesman;
Salesman.prototype.setSalary = function(salary) {
      this.salary = salary;
}
Salesman.prototype.getSalary = function() {
      return this.salary;
}
Salesman.prototype.setRaise = function() {
      return (this.salary+(this.salary*0.15));
}

function Manager() {}
Manager.prototype = new Employee();
Manager.prototype.constructor = Salesman;
Manager.prototype.setSalary = function(salary) {
      this.salary = salary;
}
Manager.prototype.getSalary = 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
0
Comment
Question by:jecommera
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37735233
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
0
 
LVL 6

Accepted Solution

by:
jjperezaguinaga earned 500 total points
ID: 37739796
Greetings jecommera,

Yeah, you can go module pattern on your Employee function :) I suggest you to read the folloowing article which explains these design patterns: http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth

var Salesman = (function (e) 
{
    e.getSalary = function() { ... };
    return e;
})(new Employee())

Open in new window


To be honest, I would dinamically identify the element an add properties according to it, overloading any previous method or saving old onews if required

var Employee = (function(e, type)
{
    var type = type || "";
    var old_method = e.getSalary;
    if(type === "manager")  e.getSalary = function() { ... };
    if(type === "salesman") e.getSalary = function() { ... };
    return e;
})(new Employee(), 'manager');

Open in new window


Instead of passing a string with a type, you can defined the prototypes of the function beforehand (using prototype, that's correct), and then use instance of inside the module function, which looks better, as you don't use strings to define your classes.

I recommend you to read a book called Javascript Design Patterns, which describes this and more javascript tricks :)
http://www.amazon.com/JavaScript-Patterns-ebook/dp/B0046RERXE/ref=sr_1_2?ie=UTF8&qid=1332191838&sr=8-2

Have a nice day!
-JJ
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question