JS高级----------------->原型添加方法解决数据共享的问题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script>
    function Person(name, sex) {
        this.name = name;
        this.sex = sex;
    }
    //通过原型来添加方法,解决数据共享的问题,节省内存空间
    Person.prototype.eat = function () {
      console.log(this.name + "在吃饭")
    };

    var per1 = new Person("Join", "");
    var per2 = new Person("Andy", "");
    per1.eat();
    per2.eat();
    console.log(per1.eat == per2.eat);
</script>
</body>
</html>

 

posted @ 2018-08-23 20:06  {颜逸}  阅读(277)  评论(0编辑  收藏  举报