javascript - Is hasPrototypeProperty(object,name) wrong on page 152 of the Little Red Book?
迷茫
迷茫 2017-06-26 10:57:58
0
2
830
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

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(2)
扔个三星炸死你

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.

淡淡烟草味

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!