javascript创建对象方式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <script type="text/javascript"> //直接创建方式 // var student = new Object(); // student.name="张斌"; // student.mobile="123456789"; // student.doHomework=function(){ // console.log(this.name+"正在做作业。。。。。"); // } // student.doHomework(); //初始化式 // var student = { // name:"阿斯顿", // mobile:"123456789", // doHomework:function(){ // console.log(this.name+"在做作业......"); // } // } // student.doHomework(); //构造方法式:可以方便的创建不同对象 // function Student(name,mobile){ // this.name=name; // this.mobile=mobile; // this.doHomework=function(){ // console.log(this.name+"做作业"); // } // } // var student = new Student("阿斯顿","215645646"); // student.doHomework(); //原型式:不能方便的创建多个对象:实现了自定义方法和构造方法分离 // function Student(){} // Student.prototype.name="阿斯顿"; // Student.prototype.mobile="1215616"; // Student.prototype.doHomework=function(){ // console.log(this.name+"正在做作业。。"); // } // var student = new Student(); // student.doHomework(); // student=new Student(); // student.name="aaaa"; // student.doHomework(); //混合式(构造函数式+原型式) function Student(name,mobile){ this.name=name; this.mobile=mobile; } Student.prototype.doHomework=function(){ console.log(this.name+"在吃屎"); } var student = new Student("许乐","211335"); student.doHomework(); </script> </body> </html>