Javascript-Object-Definition

23153 ワード

  1 /*        :    ,      ,    ,       */
  2 
  3     /**************************************/
  4     /**                                  **/
  5     /**         1.                **/
  6     /**                                  **/
  7     /**************************************/
  8 
  9     //         ,         ,  :Object,Array。
 10     var prince = new Object();
 11     prince.name = "Prince";
 12     prince.age = 25;
 13     prince.job = "Front End Engineer";
 14     prince.sayJob = function(){
 15         alert(prince.job);
 16     };
 17     var xiaobao = new Object();
 18     xiaobao.name = "Zhibao Hu";
 19     xiaobao.age = 25;
 20     xiaobao.job = "SEO";
 21     xiaobao.sayJob = function(){
 22         alert(xiaobao.job);
 23     };
 24     prince.sayJob();
 25     xiaobao.sayJob();
 26 
 27     /**************************************/
 28     /**                                  **/
 29     /**           2.                 **/
 30     /**                                  **/
 31     /**************************************/
 32 
 33     //javascript             ,   ,    ,  json         ,              。
 34     var xiaoyin = {
 35         name : "Chaohuan Yin",
 36         age : 24,
 37         job : "Wireless Yunyin",
 38         sayAge : function(){
 39             //       this    。
 40             alert(this.age);
 41         }
 42     };
 43     xiaoyin.sayAge();
 44 
 45     //         :            ,      。
 46     //
 47 
 48     /**************************************/
 49     /**                                  **/
 50     /**           3.                 **/
 51     /**                                  **/
 52     /**************************************/
 53 
 54     //                  
 55     function createPerson(name,age,job){
 56         var o = new Object();
 57         o.name = name;
 58         o.age = age;
 59         o.job = job;
 60         o.sayAge = function(){
 61             alert(this.age);
 62         };
 63         return o;
 64     }
 65     var tangtang = createPerson("Jin Shi",23,"Shop Yunyin");
 66     tangtang.sayAge();
 67     //                 ,           ,        
 68 
 69     /**************************************/
 70     /**                                  **/
 71     /**        4.                **/
 72     /**                                  **/
 73     /**************************************/
 74 
 75     function Person(name,age,job){
 76         this.name = name;
 77         this.age = age;
 78         this.job = job;
 79         this.sayName = function(){
 80             alert(this.name);
 81         };
 82     }
 83     //       
 84     var liujie = new Person("Jie Liu",27,"Yunwei");
 85     //       
 86     Person("Qinqin Yu",27,"Shop Yunyin");//   window 
 87     liujie.sayName();
 88     window.sayName();
 89     //             
 90     var o = new Object();
 91     Person.call(o,"Prince",25,"F2E");
 92     o.sayName();
 93     
 94     //             
 95     function Person1(name,age,job){
 96         this.name = name;
 97         this.age = age;
 98         this.job = job;
 99         this.sayName = sayName;
100     }
101     function sayName(){
102         alert(this.name);
103     }
104     var prince = new Person1("Prince Yu",25,"Front End Engineer");
105     var xiaoyin = new Person1("Chaohuan Yin",24,"Shop Yunyin");
106     prince.sayName();
107     xiaoyin.sayName();
108 
109     /**************************************/
110     /**                                  **/
111     /**             5.               **/
112     /**                                  **/
113     /**************************************/
114     //1.      
115     function Person2(){}
116     Person2.prototype.name = "Prince";
117     Person2.prototype.age = 29;
118     Person2.prototype.job = "F2E";
119     Person2.prototype.sayAge = function(){
120         alert(this.age);
121     };
122     var dada = new Person2();
123     var prince = new Person2();
124     dada.sayAge();
125     prince.sayAge();
126 
127     //2.         
128     function Person3(){}
129     Person3.prototype = {
130         name : "Prince",
131         age : 30,
132         job : "F2E",
133         sayAge : function(){
134         alert(this.age);
135         }
136     };
137     var wangzi = new Person3();
138     wangzi.sayAge();
139 
140     /**************************************/
141     /**                                  **/
142     /** 6.                **/
143     /**                                  **/
144     /**************************************/
145 
146     function Person4(name,age,job){
147         this.name = name;
148         this.age = age;
149         this.job = job;
150         this.friends =["Wangbing","Liutan"];
151     }
152     Person4.prototype = {
153         constructor : Person4,
154         sayAge : function(){
155         alert(this.age);
156         }
157     }
158     var dada =new Person4("Prince",25,"F2E");
159     var xiaobao =new Person4("Zhibao Hu",25,"SEO");
160     dada.friends.push("Prince Yu");
161     alert(dada.friends);
162     alert(xiaobao.friends);
163     alert(xiaobao.friends == dada.friends);
164     alert(xiaobao.sayAge == dada.sayAge);
165     //