前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Javascript创建对象的学习和使用

Javascript创建对象的学习和使用

作者头像
别先生
发布2017-12-29 17:49:34
1.5K0
发布2017-12-29 17:49:34
举报
文章被收录于专栏:别先生别先生
代码语言:javascript
复制
 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>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-09-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档