1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <title>Document</title> 7 8 <script> 9 /* 10 创建对象的语法: 11 1 new Object() 12 2 {属性名:属性值, ... ..., 函数名:function(){}} 13 */ 14 15 var person = new Object() 16 //添加属性 17 person.name = '张三' 18 person.age = 10 19 //添加方法 20 person.eat = function(food){document.write(this.age+"岁的"+this.name+"正在吃"+food)} 21 //访问属性 22 console.log(person.name) 23 console.log(person.age) 24 //调用方法 25 person.eat("火锅") 26 27 document.write("<hr>") 28 29 var person = { 30 "name":"李四", 31 "age":20, 32 "eat":function(food){ 33 document.write(this.age+"岁的"+this.name+"正在吃"+food) 34 } 35 } 36 person.eat("烤肉") 37 38 </script> 39 40 </head> 41 <body> 42 43 </body> 44 </html>
标签:07,对象,创建,age,write,person,food,eat,name From: https://www.cnblogs.com/IrVolcano/p/18074193