Javascript创建对象的学习和使用
2017-04-18 10:10
417 查看
![](https://oscdn.geek-share.com/Uploads/Images/Content/201603/69c5a8ac3fa60e0848d784a6dd461da6.gif)
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>
![](https://oscdn.geek-share.com/Uploads/Images/Content/201603/69c5a8ac3fa60e0848d784a6dd461da6.gif)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/04/86be70a8c9f0fa827e2e07c0e6da6c6a.png)
相关文章推荐
- JavaScript之面向对象学习六原型模式创建对象的问题,组合使用构造函数模式和原型模式创建对象
- JavaScript 学习_3.使用对象直接创建对象
- Javascript创建对象的学习和使用
- Javascript创建对象的学习和使用
- 【Javascript】javascript学习 三十六 创建你自己的 JavaScript 对象
- javascript---使用createElement动态创建HTML对象.窗体传值
- javascript 中的 prototype和constructor属性的使用(constructor属性始终指向创建当前对象的构造函数,每个函数都有一个默认的属性prototype,而这个prototype的constructor默认指向这个函数)
- JavaScript中使用构造器创建对象无需new的情况说明
- javascript---使用createElement动态创建HTML对象
- 使用JavaScript创建一个字符串对象
- 学习effective java-2创建和销毁对象之当类有多个构造参数时,请考虑使用一个builder来创建对象
- MOSS Search学习记录(十):MOSS Visual How To使用SharePoint Server 2007搜索对象模型编程创建搜索查询
- javascript的对象创建和使用
- JavaScript对象创建的几种方式及使用方法
- JavaScript学习笔记(四)—window对象使用
- javascript---使用createElement动态创建HTML对象.窗体传值
- 使用JavaScript创建一个字符串对象
- javascript使用大括号({})语法创建无类型对象
- 学习effective java-7创建和销毁对象之避免使用终结器(finalizers)
- javascript 直接创建对象与间接创建对象 私有函数不能访问this变量 是否使用var声明变量几个问题探讨