3.原型与原型链

通过hasOwnProperty('name') 来查看是否是自己的属性

1.原型

// class实际上是函数,可见是语法糖
typeof Student //"function"
typeof People //"function"

// 隐式原型 和 显示原型
console.log(aaa.__proto__)      //People  每个实例都有隐式原型__proto__
console.log(Student.prototype) //People  每个class都有示原型prototype
console.log(aaa.__proto__ === Student.prototype) //true  实例的隐式原型__proto__ 指向  class的示原型prototype

基本原型的执行规则

获取属性aaa,name或执行方法aaa.sayHi()时
    先在自身属性和方法寻找
    如果找不到则自动去__proto__中寻找

2.原型链

console.log(Student.prototype.__proto__) //People
console.log(People.prototype)//People
console.log(People.prototype === Student.prototype.__proto__)// true

猜你喜欢

转载自www.cnblogs.com/chenlw/p/12526382.html
今日推荐