次のようにコードします。
function Teacher(name, age) {
this.name = name;
this.age = age;
}
Teacher.prototype.sayName = function() {
alert(this.name);
};
Teacher.prototype.sayHi = function() {
alert("Hi, I'm " + this.name);
};
console.log(Teacher.prototype instanceof Teacher); // false
console.log(Teacher.prototype instanceof Object); // true
console.log(Teacher.prototype); // Teacher {sayName: function, sayHi: function}
ps 上記の出力はクロムです。最初の console.log は Teacher.prototype が Teacher のインスタンスではないことを示していますが、3 つ目の console.log は Teacher.prototype が Teacher のインスタンスであることを (直感的に) 示しており、矛盾しています。
Object.prototype は Teacher.prototype のプロトタイプ チェーンにあるため、2 番目の console.log が true であることはわかっていますTeacher.prototype.__proto__ === Object.prototype
。したがって、最初の console.log は false を出力するはずです。
しかし、3 番目の console.log の出力が Teacher.prototype が Teacher のインスタンスであることを示す理由がわかりません。誰かが私のためにそれを明確にすることができますか? どうもありがとう。