我的-原型链

20 阅读1分钟

构造行数

  • 可以new
function Person (name,age){
    this.name=name;
    this.age=age;
    this.species = '人类'this.say = function(){
        console.log('helllo')
    }
}
const p = new  Person('张三','20')

原型函数

  • 每一个函数类型的数据都有一个property属性,指向一个对象,这个就是所谓的原型对象

Person()---.property--->Persion().property

  • 对于原型对象来说,他有一个constructor属性指向它的构造函数

    • Person()---.property--->Person.property.constructor--->Person()
  • .property有什么作用?

    • 存放实例对象的公有属性和方法
    function Person(name,age){
        this.name=name;
        this.age=age;
    }
    Person.property.species = '人类';
    Person.property.say = function(){
     console.log('helllo')
    } 
    let per1 = new Person('xiaoming', 20); 
    let per2 = new Person('xiaohong', 19); 
    
    console.log(per1.species); // 人类 
    console.log(per2.species); // 人类 
    
    per1.say(); // Hello 
    per2.say(); // Hello
    
    
    
    • 可是这里的species属性和say方法不是实例对象自己的,为什么per1和per2能点运算符访问?因为对象在自己身上找不到属性和方法,就回到原型链上去找
    per1.constructor === Person()
    

    原型链

    • 显式原型链:就是用property属性找到原型,只是这个是函数类型的属性
    • 隐士原型 利用__proto__属性找到原型,这个属性指向当前构造函数的原型对象,这个属性是对象类型的属性,所以可以在实例对象上使用
      console.log(per1.__proto__ === Person.protopype)
      console.log(per2.__proto_- === Person.prototype)
      
      

image.png

  • 既然这个是对象的属性,原型对象也是对象,那么原型对象的__proto__指向谁?

image.png

image.png