JavaScript创建对象的五种方式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
var student=new Object();
student.name="jim";
student.mobile="177777777";
student.dohomework=function(){
console.log(this.name+"在做作业。。")
}
student.dohomework();
var student={
name:"jim",
mobile:"13423432",
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("jim","111");
student.dohomework();
function Student(){}
Student.prototype.name="jim";
Student.prototype.mobile="110";
Student.prototype.dohomework=function(){
console.log(this.name+"正在做作业。。");
}
var student=new Student();
student.dohomework();
function Student(name,mobile){
this.name=name;
this.mobile=mobile;
}
Student.prototype.dohomework=function(){
console.log(this.name+"做作业吧。。。")
}
var student=new Student("jim","333");
student.dohomework();
</script>
</body>
</html>