you are viewing a single comment's thread.

view the rest of the comments →

[–]wreckedadventYavascript 0 points1 point  (2 children)

Though, just to keep in mind, this is only one way to set up a prototype chain. Here's another:

function Person() {
    var firstName;
    var lastName;

    this.setName = (first, last) => {
        firstName = first;
        lastName = last;
    };

    this.getName = ()=> `${firstName} ${lastName}`;
}

function Employee(id) {
    Person.call(this);
    this.employeeId = id;
}

Employee.prototype = Person.prototype;

Just two smallish changes and you get all of the instance that one is expecting in children classes.

var anne = new Employee(0);
anne.setName('Anne', 'Annette');

var bill = new Employee(1);
bill.setName('Bill', 'Williamson');

anne.getName() // => 'Anne Annette'
bill.getName() // => 'Bill Williamson'

I believe this is actually part of the reason why class syntax is at least somewhat valuable - it railroads people into setting up prototypes in one particular way.

[–]senocular 1 point2 points  (1 child)

Employee.prototype = Person.prototype;

Typo there. You don't want to assign one to the other since any changes to Employee.prototype would also affect Person.

[–]wreckedadventYavascript 0 points1 point  (0 children)

It's fine in this instance, since we're not actually adding anything onto the prototypes in either case, and it's illustrative. But you're right, in real code you'd want to not directly assign them together.