Es5 prototype chain inheritance, subclass calling parent class method problem

the code is as follows:


function Person(name){
    this.name=name;
    this.className="person";  //
}
Person.prototype.getClassName=function(){
    console.log(this.className);
    console.log("over");
}

function Man(){
  console.log("start");
}

Man.prototype=new Person();//1    
// Man.prototype=new Man("Davin");//2
var man=new Man();//Man start
// console.log(man.getClassName());
console.log(man.getClassName());  //

output result:

start
person
over
undefined

I don"t know why I always print out a "undefined", which is printed in the sentence "console.log (man.getClassName ());"). If you comment out this sentence, you won"t have it, so how did this print come from?

Feb.28,2021

var className = man.getClassName ()
console.log (className, className = undefined);


there is no problem here. The last undefined here is
console.log (after man.getClassName ()); / / inherits, you can call the method of the parent class
getClassName does not have an argument value, so everything before returning undefined, is executed normally, and
you can use

Person.prototype.getClassName=function(){
    console.log(this.className,'');
    console.log('over','over');
    return 1;
}

look at the result.
start
person
over
1


JavaScript Advanced programming (3rd Edition), page 64, the function does not set return, and returns undefined by default.


function Person (name, className ) {

this.name=name;
this.className="person";  //

}
none of your members here are written, and then passed through-- there is no search on the proto-- prototype, so you must return undifined.

Menu