直接上示例
function options() {}
let opt = new options()
opt.name = '5555'
options.prototype.age = 26
for (const name in opt) {
console.log(name)
if (Object.prototype.hasOwnProperty.call(opt, name)) {
console.log(opt[name])
}
}
输出结果:
name
5555
age
我们会发现,age只输出了名字,没有输出值。
这说明:循环遍历对象的key值,可以获取到prototype对象,但是通过调用hasOwnProperty可以判断哪些是当前对象的key,哪些是原型链上面的key。