【发布时间】:2018-09-07 06:53:09
【问题描述】:
我相信他们有几个参考和很好的答案来解释 Javascript 中的原型,
我的问题很简单,当我创建这样的东西时。
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
Person.prototype.nationality = "English";
var myFather = new Person("John", "Doe", 50, "blue");
var myMother = new Person("Sally", "Rally", 48, "green");
console.log myFather 它显示它的名字、姓氏、年龄、眼睛颜色但没有国籍但是当我做这样的事情时..
myFather.nationality显示国籍为“英文”
现在,我在 chrome 控制台日志中运行所有内容,我的问题是为什么默认情况下国籍不会出现在 console.log 的 myFather 对象中
【问题讨论】:
-
因为它是继承的。它也不显示
myFather.constructor、myFather.hasOwnProperty或myFather.isPrototypeOf,尽管它们(以及更多)都存在:它们通常不感兴趣。
标签: javascript