0

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

3
  • I think you should review the markdown editing help page. A well-written question will get you well-written answers. Commented Jul 30, 2012 at 15:03
  • What button? You have not posted the code that calls "test2()" Commented Jul 30, 2012 at 15:04
  • <button onclick="test2()"> <p>Test 1</p> </button> Commented Jul 30, 2012 at 19:53

2 Answers 2

1

In test2() you're replacing the entire employee.prototype with an instance of person, thus overwriting the employee.prototype.show function you've defined previously with the one inherited from person. Also, as stated in the answer by codebox, in employee.prototype.show() you're not calling person.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:

employee.prototype = new person();
employee.prototype.constructor = employee;
employee.prototype.show = function() { ... }

Also, when you call your parent's method, you need to supply the correct context yourself:

person.prototype.show.call(this);
Sign up to request clarification or add additional context in comments.

2 Comments

@user1343454: Code ordering is important. What you're doing to employee.prototype is like: array=[1]; array.push(2); array=[]; array.push(3);.
Thanks. Lanzz suggestion helped.
0

In employee.prototype.show you are not calling the person.prototype.show method - change it to this:

employee.prototype.show = function() {
     person.prototype.show();  
     alert ("override2");
 }

4 Comments

The method will get person.prototype as this which is really unexpected.
Your funtion seems to be the same I have written in my code :(
Done but I still see only overide1 alert message :(
employee.prototype.show = function() { alert ("override2"); person.prototype.show(); };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.