构造函数,经过原型添加方法,如下语法,手动修改构造器的指向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>