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>
相關文章
相關標籤/搜索