2

I am studying ways to apply Object Orientation in JavaScript.
I found a solution to use inheritance, I wonder if there is better ways and how to wrap my classes.

That's what I've done so far.

People = function (name) {
    this.name = name
    this.age = null;
};

Employee = function (name) {
    People.call(this, name);
    this.IdentificationCode = null;
    this.salary = null;
}

Jonh = new Employee("Jonh Smith");
Jonh.age = 25;
Jonh.IdentificationCode = 35632;
Jonh.salary = 3500;
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • 3
    This question appears to be off-topic because it is about working code, it should be migrated to http://codereview.stackexchange.com/ – Pavlo Feb 25 '14 at 14:31
  • The call part runs the instance specific part of inheritance. You can inherit behavior through prototype for more info please read the following answer.http://stackoverflow.com/a/16063711/1641941 – HMR Feb 25 '14 at 15:16

4 Answers4

3

Note: You are not inheriting from People, but you are reusing People's constructor function.

Suggestion 1:

Make sure that you are not creating global variables.

var People = function (name) {     // var at the beginning is important
...
...
var Employee = function (name) {   // var at the beginning is important
...
...
var Jonh = new Employee("Jonh Smith");

Suggestion 2:

The constructor function should have a way to initialize other variables as well.

var People = function (name, age) {
    this.name = name || null;
    this.age  = age || null;
};
var Employee = function (name, age, idCode, salary) {
    People.call(this, name, age);
    this.IdentificationCode = idCode || null;
    this.salary = salary || null;
}

As People doesn't have any methods in its prototype, this should be fine.

But if you have methods in People's prototype and you want them to be available to your derived objects as well, you can do this

var People = function (name, age) {
    this.name = name || null;
    this.age  = age || null;
};

People.prototype.getData = function() {
    return [this.name, this.age];
};

Now define Employee like this

var Employee = function (name, age, idCode, salary) {
    People.call(this, name, age);
    this.IdentificationCode = idCode;
    this.salary = salary;
}

// Make the Employee's prototype an object of parent class's prototype
Employee.prototype = Object.create(People.prototype);

Then do,

var Jonh = new Employee("Jonh Smith", 25, 35632, 3500);
console.log(Jonh.getData());

Now, it will invoke People's getData and will print

[ 'Jonh Smith', 25 ]

Note: This type of inheritance is normally called as Prototypal inheritance.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
2
function Person(name, age) {
    this.name = name;
    this.age = age;
}


function Employee(name, age) {
    Person.call(this, name, age);
    this.salary = null;
}


Employee.prototype = Object.create(Person.prototype);

var teste = new Employee("Felipe",25)
teste instanceof Employee // true
teste instanceof Person // true

Object.create is making heritage. Object.create receives an object and returns another whose prototype is the object passed.

Tuyoshi Vinicius
  • 861
  • 8
  • 22
1

You can set Employee to inherit from People using Object.create.

var People = function (name) {
    this.name = name
    this.age = null;
};

var Employee = function (name) {
    People.call(this, name);
    this.IdentificationCode = null;
    this.salary = null;
}

Employee.prototype = Object.create(People.prototype); // create a new object inheriting from People.prototype
Employee.prototype.constructor = Employee; // put the constructor back

var Jonh = new Employee("Jonh Smith");
Jonh.age = 25;
Jonh.IdentificationCode = 35632;
Jonh.salary = 3500;
Scimonster
  • 32,893
  • 9
  • 77
  • 89
0

You can do it in many ways. One is the same you did. Another is to use the prototype object:

Employee.prototype = new People();

You can also use functions that return newly created objects, and call one from another:

function getPeople ( name ) {
    var result;
    result.name = name;
    result.age = null;

    return result;
}

function getEmployee ( name ) {
    var result = getPeople ( name );
    result.IdentificationCode = null;
    result.salary = null;

    return result;
}
Buch
  • 92
  • 3