1 <html>
2 <head>
3 <meta charset="utf-8">
4 <title>javascript对象的学习</title>
5 </head>
6 <body>
7 <h1>1.使用JS创建person对象,里面有id,name,age,sex属性 ,有eat,run方法 (2种方法创建)</h1><hr>
8 <script language="javascript">
9 document.write("<h2>"+"第一种方法"+"</h2>");
10 var person=new Object();
11 person.id="10010";
12 person.name="小别";
13 person.age=22;
14 person.sex="男";
15 person.eat=function(){
16 document.write("eat()方法:男的喜欢吃高热量的食物!");
17 }
18 person.run=function(){
19 document.write("run()方法:程序员要自觉锻炼身体哟!");
20 }
21 document.write("编号:"+person.id+"<br/>");
22 document.write("姓名:"+person.name+"<br/>");
23 document.write("年龄:"+person.age+"<br/>");
24 document.write("性别:"+person.sex+"<br/>");
25 person.eat();
26 document.write("<br/>");
27 person.run();
28 document.write("<br/>");
29 document.write("<h2>"+"第二种方法"+"</h2>");
30 var person=new Person("10011","小李",23,"男");
31 function Person(id,name,age,sex){
32 this.id=id;
33 this.name=name;
34 this.age=age;
35 this.sex=sex;
36 this.eat=function(){
37 document.write("eat()方法:女的应该吃低热量的食物哟!");
38 }
39 this.run=function(){
40 document.write("run()方法:女程序员也要自觉锻炼身体哟!");
41 }
42 }
43 document.write("编号:"+person.id+"<br/>");
44 document.write("姓名:"+person.name+"<br/>");
45 document.write("年龄:"+person.age+"<br/>");
46 document.write("性别:"+person.sex+"<br/>");
47 person.eat();
48 document.write("<br/>");
49 person.run();
50 document.write("<br/>");
51 document.write("<h2>"+"第三种:创建对象使用最多的方法"+"</h2>");
52 var person={id:"10012",name:"小赵",age:24,sex:"男",eat:function(){
53 document.write("eat():男的女的都喜欢吃好的");
54 },run:function(){
55 document.write("run():男的女的都懒哟!所以要自觉!");
56 }};
57 document.write("编号:"+person.id+"<br/>");
58 document.write("姓名:"+person.name+"<br/>");
59 document.write("年龄:"+person.age+"<br/>");
60 document.write("性别:"+person.sex+"<br/>");
61 person.eat();
62 document.write("<br/>");
63 person.run();
64 document.write("<br/>");
65 </script>
66
67 </body>
68 </html>