var Animal = function (){
} var property;
Animal.prototype.name='jason';
for( property in Animal.prototype ){
alert(property);
}为什么property只弹出了jason。
Animal.prototype 内置的constructor属性为什么弹不出来。用alert(Animal.prototype.constructor)
却可以看到。
} var property;
Animal.prototype.name='jason';
for( property in Animal.prototype ){
alert(property);
}为什么property只弹出了jason。
Animal.prototype 内置的constructor属性为什么弹不出来。用alert(Animal.prototype.constructor)
却可以看到。
var Animal = function (){
}var property;
Animal.prototype.name='jason'; //通过prototype产生属性
Animal.prototype.fun01=function(){alert("function 01");}; ////通过prototype产生函数
for( property in Animal.prototype ){
alert(property);
}可以显示jason,fun01,但不显示Animal的原型定义
name为什么不算原型链当中的属性name已经被添加到了原型链之中啊
alert(p + ' = ' + animal[p]); // 这里只会枚举出name属性,而不含Object当中的属性。
}
}
貌似只要不是自己的都不会被for in枚举出来。
虽然我也不知道为什么constructor不会被枚举出来,for in绝对是要看原型链的。
hasOwnProperty不看原型链。不要误导别人。
var Animal = function (){
}var property;
Animal.prototype.name='jason'; //通过prototype产生属性
Animal.prototype.fun01=function(){alert("function 01");}; ////通过prototype产生函数
for( property in Animal.prototype ){
alert(property);
}显示name,fun01,但不显示Animal的原型定义
(function(){
var Animal = function(){
};
Animal.prototype.inProtoType = {
name: "inPrototype"
};
var aInstanceOfAnimal = new Animal();
aInstanceOfAnimal.notInPropertyType = {
name: "notInPropertyType"
};
for (name in aInstanceOfAnimal) {
document.write("name:"+name);
document.write("<br />");
if (aInstanceOfAnimal[name]) {
document.write("value:")
document.write(aInstanceOfAnimal[name]["name"]);
document.write("<br />")
}
if (aInstanceOfAnimal.hasOwnProperty(name)) {
document.write("can be get by hasOwnProperty");
document.write("<br />");
}
else {
document.write("can not be get by hasOwnProperty");
document.write("<br />");
}
}
}())
这段示例代码演示了属性在原型链的过程中, for in可以检索, 而hasOwnProperty不能检索出来。lz你这样做其实没有用到原型链, 因为你一直在操作prototype对象, 没有去操作Animal对象。所以不涉及任何原型链的问题。
只是简单的赋值, 取值。
如果 proName 存在于 object 中且可以使用一个 For…In 循环穷举出来,那么 propertyIsEnumerable 属性返回 true。如果 object 不具有所指定的属性或者所指定的属性不是可列举的,那么 propertyIsEnumerable 属性返回 false。典型地,预定义的属性不是可列举的,而用户定义的属性总是可列举的。
js手册上的,这里可能解释了为什么constructor不能被枚举出来
for (var prop in arr) {
alert(prop);
}
Array中属性也是预定义属性吧。
还有一类叫系统属性,比如prototype.constructor,这类属性不可以被delete关键字删除。