function hasProtoproperty(object,name){
return !object.hasOwnproperty(name) && (name in object);
}
Red Book P152 says that this function can detect whether the attribute is in the prototype or in the instance
true在原型中
false在实例中
However, when a certain attribute does not exist in the prototype or instance, it will also return false
扔个三星炸死你2017-06-26 10:59:58
After flipping through the book, there is nothing wrong with the function, but the explanation in the book is indeed easy to cause misunderstanding.
Just look at the function name - hasPrototypeProperty(object, name)
- The original meaning is to determine whether there is a specified attribute on the prototype of an object. As long as there is no specified attribute on the prototype, false will be returned.
I understand that the explanation in the book is for the attribute name
. It is based on the premise that the object has this attribute. If the function returns false, then this attribute is on the prototype.
淡淡烟草味2017-06-26 10:59:58
I tried it just now, and it is indeed true. Maybe the book just emphasizes the differences and methods. It will be perfect if you add judgment when you use it yourself