1. 程式人生 > >JS高級----------------->原型簡單的寫法(註意手動修改構造器的指向)

JS高級----------------->原型簡單的寫法(註意手動修改構造器的指向)

需要 htm -c 手動 idt 註意 log fun clas

<!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>

JS高級----------------->原型簡單的寫法(註意手動修改構造器的指向)