構造函數,經過原型添加方法,如下語法,手動修改構造器的指向html
實例化對象,並初始化,調用方法函數
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>title</title> <script> function Student(name, age, sex) { this.name = name; this.age = age; this.sex = sex; } //簡單的原型寫法 Student.prototype = { //手動修改構造器的指向 constructor: Student, height: "188", weight: "70kgs", study: function () { console.log("學習12小時"); }, eat: function () { console.log("吃午餐"); } }; //實例化對象,並初始化 var stu = new Student("段飛", 20, "男");
//調用方法 stu.eat(); stu.study(); console.dir(stu); console.dir(Student); </script> </head> <body> </body> </html>