JS_dom_自定义对象

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>自定义对象</title>
<script type="text/javascript">
//1.采用直接量方式创建对象
function f1() {
var student=
{"name":"zs","age":"23","work":function(){
alert("我学Java");
}
}
alert(student.name);
alert(student.age);
student.work();
}
//2.构造器(new的函数/首字母大写)

//1.内置构造器
function f2() {
var teacher= new Object();
teacher.name="唐长老";
teacher.age=18;
teacher.work=function(){
alert("我要取经");
}
alert(teacher.name);
alert(teacher.age);
teacher.work();
}
//2.自定义构造器
function Coder(name,age,work){
this.name=name;
this.age=age;
//this 指代当前对象
//.job是给此对象增加job属性
//=work是将参数work赋值给此属性
this.job=work;
}
function f3() {
var coder= new Coder("lisi",25,
function(){alert("我学习Java");
});
alert(coder.name);
alert(coder.age);
coder.job();
}
</script>
</head>
<body>
<input type="button" value="按钮1" onclick="f1();">
<input type="button" value="按钮2" onclick="f2();">
<input type="button" value="按钮3" onclick="f3();">
</body>
</html>

 

posted @ 2017-10-09 10:32  Big_hua  阅读(265)  评论(0编辑  收藏  举报