一、直接创建对象
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
//直接创建对象
var person = new Object();
person.name = "Lucy";
person.study=function(){
console.log(this.name + "正在学习")
}
person.study();
</script>
</body>
</html>
二、初始化器方式
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
//初始化器方式
var student={
name:"Tom",
doHomework:function(){
console.log(this.name + "正在做作业")
},
study:function(age) {
console.log(this.name +"年龄" + age + "正在研究");
}
}
student.doHomework();
student.study("123");
</script>
</body>
</html>
三、构造方法式
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
//构造方法式
function Man(name){
this.name = name;
this.study = function(){
console.log(this.name + "在学习");
}
}
var man = new Man("Timmy");
man.study();
</script>
</body>
</html>
四、原型式
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
//原型式
function Woman() {}
Woman.prototype.name="Jim";
Woman.prototype.study=function() {
console.log(this.name + "正在努力学习");
}
var woman = new Woman();
woman.study();
</script>
</body>
</html>
五、混合式
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
//混合式
function People(name) {
this.name=name;
}
People.prototype.study = function(){
console.log(this.name + "正在研究");
}
var people = new People("张三");
people.study();
</script>
</body>
</html>
注意:其中常用的创建对象的方法为直接创建对象和混合式。