1、直接创建式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script>
//直接创建式
var student= new Object();
student.name="zzs";
student.study=function(name){
console.log(this.name+"正在学习。。。");
};
student.study(name);
</script>
</head>
<body>
</body>
</html>
在浏览器的控制台查看输出结果:
2、对象初始化
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script>
//对象初始化
var student = {
name:"zzs",
study:function(name){
console.log(name+"正在学习");
}
}
student.study("zcs");
console.log(student.name);
</script>
</head>
<body>
</body>
</html>
在浏览器的控制台查看输出结果:
3、原型式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script>
//原型式
function Student(){
}
Student.prototype.name="zcs";
Student.prototype.study=function(){
console.log(this.name+"正在学习。。。")
}
var student=new Student();
student.study();
</script>
</head>
<body>
</body>
</html>
在浏览器的控制台查看输出结果:
4、构造方法式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script>
//构造方法式
function Student(name){
this.name=name;
this.study=function(){
console.log(this.name+"正在学习。。。")
}
}
var student=new Student("zzs");
student.study()
</script>
</head>
<body>
</body>
</html>
在浏览器的控制台查看输出结果:
5、混合式(原型+构造方法)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script>
//混合式(原型+构造方法)
function Student(name){//使用构造方法指定属性
this.name=name;
}
Student.prototype.study=function(){//使用原型式定义函数
console.log(this.name+"正在学习。。。")
}
var student=new Student("zzs");
student.study();
</script>
</head>
<body>
</body>
</html>
在浏览器的控制台查看输出结果: