原型的指向是否可以改变 原型最终指向了哪里 原型指向改变如何添加方法和访问

原型的指向是否可以改变

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script>
     //  function Student(){

     //  }
     //  Student.prototype.study = function(){
     //      console.log("就是天天学习");
     //  };
     // Student.prototype = {
     //      eat:function(){
     //          console.log("哈哈,好吃的榴莲");
     //      }
     // };

     // var stu = new Student();
     // stu.eat();
     // stu.study();

     // 人的构造函数
     function Person(age){
        this.age = 10;
     }
     // 人的原型对象方法
     Person.prototype.eat = function(){
        console.log("人的吃");
     };
     // 学生的构造函数
     function Student(){

     }
     Student.prototype.sayHi = function(){
        console.log("小苏你好帅哦");
     };
     // 学生的原型,指向了一个人的实例对象
     Student.prototype = new Person(10);
     var stu = new Student();
     stu.eat();
     // stu.say();

     // 原型指向可以改变
     // 实例对象的原型__proto__指向的是该对象所在的构造函数的原型对象
     // 实例对象的原型__proto__
     // 构造函数的原型(prototype)如果改变了,实例对象的原型(__proto__)指向
     // 也会发生改变

     // 原型的指向也是可以改变的
     // 实例对象和原型对象之间的关系是通过__proto__原型来联系起来的,这个关系就是
     // 原型链


  </script>
</head>
<body>
  
</body>
</html>

原型最终指向了哪里

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script>
      function Person(){

      }
      Person.prototype.eat = function(){
          console.log("吃东西");
      };

      var per = new Person();
      // console.dir(per);
      // console.dir(Person);

      // 实例对象中有__proto__原型
      // 构造函数中有prototype原型
      // prototype是对象
      // 所以,prototype这个对象中也有__proto__,那么指向了哪里
      // 实例对象中的__proto__指向的是构造函数的prototype
      // 所以,prototype的这个对象中的__proto__指向的应该是某个构造函数的原型
      // prototype

      // Person的prototype中的__proto__的指向
      // console.log(Person.prototype.__proto__);

      // 实例对象__proto__---->Person.prototype的__proto__---->Object.prototype
      // 的__proto__是null
      console.log(per.__proto__==Person.prototype);
      console.log(per.__proto__.__proto__==Person.prototype.__proto__);
      console.log(Person.prototype.__proto__==Object.prototype);
      console.log(Object.prototype.__proto__);
  </script>
</head>
<body>
  
</body>
</html>

原型指向改变如何添加方法和访问

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script>
      // // 人的构造函数
      // function Person(age){
      //     this.age = age;
      // }
      // // 人的原型中添加方法
      // Person.prototype.eat = function(){
      //     console.log("人正在吃东西");
      // };
      // // 学生构造函数
      // function Student(sex){
      //     this.sex = sex;
      // };
      // // 改变了原型对象的指向
      // Student.prototype = new Person(10);
      // // 学生的原型中添加方法----现在原型中添加方法
      // Student.prototype.sayHi = function(){
      //     console.log("您好");
      // };

      // var stu = new Student("男");
      // stu.sayHi();
      // stu.eat();

      // // 人的构造函数
      // function Person(age){
      //     this.age = age;
      // }
      // // 人的原型中添加方法
      // Person.prototype.eat = function(){
      //     console.log("人正在吃东西");
      // };
      // // 学生构造函数
      // function Student(sex){
      //     this.sex = sex;
      // };
      // // 改变了原型对象的指向
      // Student.prototype = new Person(10);
      // // 学生的原型中添加方法----现在原型中添加方法
      // Student.prototype.sayHi = function(){
      //     console.log("您好");
      // };

      // var stu = new Student("男");
      // // stu.sayHi();
      // // stu.eat();

      // console.dir(stu);

      // 如果原型指向改变了,那么就应该在原型改变指向之后添加原型方法

      function Person(age){
          this.age = age;
      }


      // 指向改变了
      Person.prototype = {
          eat:function(){
            console.log("吃");
          }
      };

      // 先添加原型方法
      Person.prototype.sayHi = function(){
          console.log("您好");
      };
      
      var per = new Person(10);
      per.sayHi();

  </script>
</head>
<body>
  
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值