原型的簡單的語法
建構式,通過原型添加方法,以下語法,手動修改構造器的指向
實體化物件,并初始化,呼叫方法
<!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>



轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/159144.html
標籤:JavaScript
上一篇:JS高級---利用原型共享資料
