博客地址:http://www.globm.top/blog/1/detail/25
node api 请求时返回数据对象原型为空,所以就不存在 Object.prototype 上面的方法 hasOwnProperty()

[Object: null prototype] {
  user_name: 'xxxxxx',
  password: 'xxxxxx',
  email: '151xxxxxxxx@163.com' }

这时候可以通过使用 call / apply 强行让对象使用方法

hasOwnProperty():Object.prototype.hasOwnProperty.call(ObjectInstance, attribute)
// 字面量
const foo = {
    a: 1
}
console.log(foo.hasOwnProperty('a')) // true
// Object.create(null) 空原型链
const obj = Object.create(null)
obj.a = 1
 // Uncaught TypeError: bar.hasOwnProperty is not a function
console.log(obj.hasOwnProperty('a'))
// 显示绑定
console.log(Object.prototype.hasOwnProperty.call(obj, 'a')) // true
Logo

脑启社区是一个专注类脑智能领域的开发者社区。欢迎加入社区,共建类脑智能生态。社区为开发者提供了丰富的开源类脑工具软件、类脑算法模型及数据集、类脑知识库、类脑技术培训课程以及类脑应用案例等资源。

更多推荐