Func2 is called when I click on button . Why i don’t get any popup? Should I see both alert alert (“override1”) and alert (“override2”) after the first one?
// JavaScript Document
function person(name, surname) {
this.name = "";
this.surname = "";
this.age = "11";
this.setName(name);
this.setSurname(surname);
//alert('Person instantiated');
}
person.prototype.setName = function(name) {
this.name = "Sir1" + name;
}
person.prototype.setSurname = function(surname) {
this.surname = "-" + surname;
}
person.prototype.setAge = function(newAge) {
this.age = newAge;
}
person.prototype.show = function() {
alert("override1");
}
function employee(name, surname, company) {
//if (arguments[0] === inheriting) return;
person.call(this, name, surname); // -> override del costruttore
//this.name = "Sir2"+name;
this.company = company;
};
employee.prototype.show = function() {
person.prototype.show;
alert("override2");
}
function test2() {
employee.prototype = new person();
// correct the constructor pointer because it points to Person
employee.prototype.constructor = employee;
// Crea un oggetto impiegato da persona
impiegato = new employee("Antonio", "Di Maio", "Consuldimo");
//impiegato.show();
impiegato.show();
}
Thanks
In
test2()you’re replacing the entireemployee.prototypewith an instance ofperson, thus overwriting theemployee.prototype.showfunction you’ve defined previously with the one inherited fromperson. Also, as stated in the answer by codebox, inemployee.prototype.show()you’re not callingperson.prototype.show(), but merely evaluating it in void context, which has no effect at all.You’ll have to set
employee‘s parent before you define any additional methods on its prototype:Also, when you call your parent’s method, you need to supply the correct context yourself: