I've checked questions: javascript what is property in hasOwnProperty? and javascript hasOwnProperty and prototype but couldn't find the answer to my problem. Here is my code: But some answers are puzzling me (NOT as expected).
function School(schoolName) {
this.schoolName = schoolName;
}
School.prototype.printSchoolName = function() {
console.log(this.schoolName);
}
function Student(studentName, schoolName) {
this.studentName = studentName;
this.schoolName = schoolName; // alternative : School.call(this, schoolName);
}
Student.prototype = new School(); // force l'héritage des propriétés de School
Student.prototype.printStudentName = function() {
console.log(this.studentName);
}
var s = new Student("Victor", "IUT");
s.printStudentName(); // works fine
s.printSchoolName(); // works fine
console.log(Student.prototype.hasOwnProperty("printStudentName")); // works as expected: true
console.log(Student.prototype.hasOwnProperty("printSchoolName")); // works as expected: false
console.log(Student.prototype.hasOwnProperty("studentName")); // NOT as expected: false
console.log(School.prototype.hasOwnProperty("schoolName")); // NOT as expected: false
console.log(Object.getOwnPropertyNames(new School())); // schoolName
console.log(Object.getOwnPropertyNames(new Student())); // studentName, schoolName
The last 2 alerts don't mention the methods, though these methods have been detected by hasOwnProperty. Puzzling. Thank you.