案例一:使用js的构造方法生成js对象、给该对象赋属性值或function
案例二:使用json的代码风格,完成同样的功能。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/jquery-1.8.3.js" ></script>
<script type="text/javascript"> //案例一
function run1(name,age,sayHello){
this.run1_name = name;
this.run1_age = age;
this.run1_init = sayHello;
}
var ex1 = new run1("pecool",27,function(){
alert("hi hello haha");
})
alert("案例一:"+ex1.run1_age);
alert("案例一:"+ex1.run1_name);
ex1.run1_init(); //案例二
var run2 =
{
name : undefined,
age : undefined,
sayHello : undefined
} run2.name = "pecool";
run2.age = "27";
run2.sayHello = function(){
alert("say hello!!");
}
alert("案例二:"+run2.name);
alert("案例二:"+run2.age);
run2.sayHello(); </script>
<title></title>
</head>
<body> </body>
</html>