<!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="15639366888";
student.doHomework=function(){
console.log(this.name+"正在做作业......");
}
student.doHomework();
//初始化式
var student = {
"name":"Jim",
mobile:"15639366888",
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","15639366888");
student.doHomework();
var student1 = new Student("jhon","15869658668");
/* 原型式:不能方便的创建多个对象:实现了自定义方法和构造方法的分离 */
function Student(){
Student.prototype.name="Jim";
Student.prototype.mobile="15639366888"
Student.prototype.doHomework=function(){
console.log(this.name+"正在做作业......");
}
}
var student = new Student();
student.name="Lucy";
student.doHomework();
/* 混合式(构造函数式+原型式) */
function Student(name,mobile){
this.name=name;
this.mobile=mobile;
}
Student.prototype.doHomework=function(){
console.log(this.name+"正在做作业......");
}
var student = new Student("Lily","120");
student.doHomework();
</script>
</body>
</html>