【发布时间】:2015-12-26 10:34:07
【问题描述】:
我知道继承的属性是不可枚举的。在下面的代码中,stanford 从另一个对象university 继承了一个属性level。
但是当我枚举stanford 时,我看到继承的属性也被列出。请解释一下(我一定是哪里错了)。
var university = {level:"bachelor"};
var stanford = Object.create(university);
stanford.country = "USA";
stanford.rating = "Good";
console.log(Object.hasOwnProperty("level")); // false
for(var properties in stanford){ // level, country, rating
console.log(properties);
}
【问题讨论】:
-
因为
for..in会遍历原型链中的所有属性。见developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…。
标签: javascript inheritance enumeration