【问题标题】:Why Object.prototype.hasOwnProperty is Not equal to the instance.__proto__.hasOwnProperty为什么 Object.prototype.hasOwnProperty 不等于 instance.__proto__.hasOwnProperty
【发布时间】:2020-08-15 09:56:17
【问题描述】:

这是我的代码、我的类、我的实例

语言:javascript

class People {
    constructor (name) {
        this.name = name
    }
    eat () {
        console.log(`${this.name} eat something`)
    }
}

class Student extends People {
    constructor (name, number) {            
        super(name)
        this.number = number
    }
    learn () {
        console.log(`student ${this.name} is learning`)
    }
}

class Teacher extends People {
    constructor (name, major) {
        super(name)
        this.major = major
    }
    teach () {
        console.log(`${this.name} teacher ${this.major}`)
    }
}

// instance
const xialuo = new Student('xialuo-name', 100)

enter image description here

为什么我在 Chrome 中执行代码时会出现这种情况。

xialuo.__proto__.__proto__.__proto__.hasOwnProperty === Object.prototype.hasOwnProperty // true
Object.prototype.hasOwnProperty === xialuo.__proto__.__proto__.__proto__.hasOwnPerperty // false

发生了什么???

为什么上一行代码返回true。

下一行代码返回 false ???

我很困惑

【问题讨论】:

    标签: javascript class google-chrome prototype proto


    【解决方案1】:

    有错字问题hasOwnPerperty

    改变

    Object.prototype.hasOwnProperty === xialuo.__proto__.__proto__.__proto__.hasOwnPerperty
    

    Object.prototype.hasOwnProperty === xialuo.__proto__.__proto__.__proto__.hasOwnProperty
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 2013-08-23
      • 2012-03-16
      • 2017-05-23
      相关资源
      最近更新 更多