JS高级---原型中的方法是可以相互访问(调用)的

原型中的方法是可以相互访问(调用)的

原型中的方法,是可以相互访问,相互调用
 
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>title</title>
  <script>
    //原型中的方法,是可以相互访问的
    function Animal(name, age) {
      this.name = name;
      this.age = age;
    }

    //原型中添加方法
    Animal.prototype.eat = function () {
      console.log("吃竹子");
      this.play();
    };

    Animal.prototype.play = function () {
      console.log("玩球");
      this.sleep();
    }

    Animal.prototype.sleep = function () {
      console.log("睡觉了");
    }

    //实例化并初始化
    var panda = new Animal("盼盼", 9);
    panda.eat();//调用方法
    //原型对象中的方法,可以相互调用
  </script>
</head>

<body>


</body>

</html>
原文地址:https://www.cnblogs.com/jane-panyiyun/p/12109309.html