JS-08-对象
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <a href="javascript:void(0)" onclick="obj.say();" style="text-decoration: none;">点我</a> 9 10 <script type="text/javascript"> 11 // 通过系统内置的object()定义对象 12 var obj = new Object(); 13 obj.name = '张三'; 14 obj.sex = 'boy'; 15 obj.age = '22'; 16 obj.say = function(){ 17 console.log('I am ',obj.name,',a ',obj.sex,',I am ',obj.age,'years old.'); 18 }; 19 20 // 直接定义对象 21 var obj1={ 22 name:"李四",/*此处用逗号*/ 23 sex:'girl',/*此处用逗号*/ 24 age:18,/*此处用逗号*/ 25 say:function(){ 26 console.log('I am ',obj1.name,',a ',obj1.sex,',I am ',obj1.age,'years old.'); 27 } 28 }; 29 obj1.say(); 30 31 // 通过构造函数定义对象 32 function pen(name,sex){ 33 this.name = name; 34 this.sex = sex; 35 this.say = function(){ 36 console.log('I am ',this.name,',a ',this.sex); 37 } 38 } 39 var obj3 = new pen("王五",'boy'); 40 41 console.log(obj3.constructor); 42 </script> 43 <br /> 44 <a href="javascript:void(0)" onclick="obj3.say();" style="text-decoration: none;">再点我</a> 45 46 </body> 47 48 </html>
可以的话,记得帮我点个推荐,然后收藏+关注哟