hasOwnProperty的问题
来源:5-22 【TS继承源码】逐行深剖,手写TS继承JS源码-4

宝慕林4313846
2021-08-27
老师您好, 为什么不直接写成People.hasOwnProperty(key) 而要写成
Object.prototype.hasOwnProperty.call呢? 是考虑运行性能吗?
写回答
1回答
-
function People () {
this.name = "daha"
}
People.xx = "sdf"
// 主要是查找hasOwnProperty上的性能考虑
// 方法1:向上一级Function.prtotype->再上一级Object.prototype原型查找hasOwnProperty,
// 需要损耗一点性能
console.log(People.hasOwnProperty("xx"))//true
// 方法2: 向上一级Object原型查找hasOwnProperty,需要损耗一点性能
console.log(People.prototype.hasOwnProperty.call(People, "xx"));//true
// 方法3: 向上一级Object原型查找hasOwnProperty,需要损耗一点性能
console.log(Function.prototype.hasOwnProperty.call(People, "xx"));//true
// 方法4:
console.log(Object.prototype.hasOwnProperty.call(People, "xx"));//true
022021-09-08
相似问题