JS高级----------------->原型简单的写法(注意手动修改构造器的指向)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script>
    function Student(name, age, sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    //简单的原型写法
    Student.prototype = {
        //手动修改构造器的指向
        constructor: Student,
        height: 188,
        width: 55,
        study: function () {
            return this.name + "在学习";
        },
        eat: function () {
            return this.name + "在吃饭";
        }
    };

    var stu = new Student("Andy", 18, "");
    console.log(stu.study());
    console.log(stu.eat());
    console.dir(stu);
    console.dir(Student);

    //注意需要手动修改构造器的指向
</script>
</body>
</html>

 

posted @ 2018-08-24 03:29  {颜逸}  阅读(478)  评论(0编辑  收藏  举报