<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
//json
function f1(){
var stu={
"name":"zhangsan","age":23,
"work":function(){
alert("学生时代");
}
};
alert(stu.name);
alert(stu.age);
stu.work();
}
//内置构造器
function f2(){
var teacher = new Object();
teacher.name = "lisi";
teacher.age = 25;
teacher.work = function(){
alert("我是老师");
};
alert(teacher.name);
alert(teacher.age);
teacher.work();
}
//自定义构造器。声明函数,首字母大写,让对象记录这些参数
function Coder(name,age,work){
this.name = name;
this.age = age;
this.job = work;
}
function f3(){
var coder = new Coder("王五",26,function(){alert("我是校长");});
alert(coder.name);
alert(coder.age);
coder.job();
}
</script>
</head>
<body>
<input type="button" value="按钮1" οnclick="f1();" />
<input type="button" value="按钮2" οnclick="f2();" />
<input type="button" value="按钮3" οnclick="f3();" />
</body>
</html>
js对象
最新推荐文章于 2019-11-25 22:19:36 发布