58 lines
1.4 KiB
HTML
58 lines
1.4 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Document</title>
|
|
</head>
|
|
|
|
<body>
|
|
<script>
|
|
// 1. 字面量创建对象
|
|
var ahuang = {
|
|
kind: "dog",
|
|
food: "杂食",
|
|
kanjia: function () {
|
|
console.log("汪汪汪")
|
|
},
|
|
chaijia: function () {
|
|
console.log("二哈的胜利!")
|
|
}
|
|
}
|
|
|
|
// 2.工厂模式
|
|
function create(kind,food,kanjia,chaijia){
|
|
var newobj={}
|
|
newobj.kind=kind
|
|
newobj.food=food
|
|
newobj.kanjia=kanjia
|
|
newobj.chaijia=chaijia
|
|
return newobj
|
|
}
|
|
|
|
var fugui=create("dog","啥都吃",function(){
|
|
console.log("汪汪队立大功")
|
|
},function (){
|
|
console.log("狗肉店见")
|
|
})
|
|
|
|
console.log(ahuang)
|
|
console.log(fugui)
|
|
|
|
|
|
// 3.构造函数
|
|
function creator(kind,food,kanjia,chaijia){
|
|
this.kind=kind;
|
|
this.food=food;
|
|
this.kanjia=kanjia;
|
|
this.chaijia=chaijia
|
|
}
|
|
|
|
var dali= new creator("dog","杂食",function(){console.log("dali会看家")},function (){console.log("大力不拆家")})
|
|
console.log(dali)
|
|
</script>
|
|
</body>
|
|
|
|
</html> |