es5中类与继承

  1 <!doctype html>
  2 <html>
  3     <head>
  4         <meta charset="utf-8">
  5         <meta name="description" content="">
  6         <meta name="viewport" content="width=device-width, initial-scale=1">
  7 
  8         <script>
  9 
 10 
 11  // es5里面的类  
 12         
 13 
 14 //1.最简单的类
 15         // function Person(){
 16 
 17         //     this.name='张三';
 18         //     this.age=20;
 19         // }
 20         // var p=new Person();
 21         // alert(p.name);
 22 
 23 
 24 
 25 //2、构造函数和原型链里面增加方法
 26 
 27 
 28         // function Person(){
 29 
 30         //     this.name='张三';  /*属性*/
 31         //     this.age=20;
 32         //     this.run=function(){
 33 
 34         //         alert(this.name+'在运动');
 35         //     }
 36 
 37         // }
 38         // //原型链上面的属性会被多个实例共享   构造函数不会
 39         // Person.prototype.sex="男";
 40         // Person.prototype.work=function(){
 41         //     alert(this.name+'在工作');
 42         
 43         // }
 44         // var p=new Person();
 45         // // alert(p.name);
 46         // // p.run();
 47         // p.work(); //调用原型方法
 48 
 49 
 50 
 51 //3类里面的静态方法
 52 
 53 
 54         // function Person(){
 55 
 56         //     this.name='张三';  /*属性*/
 57         //     this.age=20;
 58         //     this.run=function(){  /*实例方法*/
 59 
 60         //         alert(this.name+'在运动');
 61         //     }
 62 
 63         // }
 64         //声明静态方法    
 65         // Person.getInfo=function(){
 66 
 67         //     alert('我是静态方法');
 68         // }
 69         // //原型链上面的属性会被多个实例共享   构造函数不会
 70         // Person.prototype.sex="男";
 71         // Person.prototype.work=function(){
 72         //     alert(this.name+'在工作');
 73 
 74         // }
 75         // var p=new Person();    
 76         // p.work();
 77 
 78         // //调用静态方法
 79         // Person.getInfo();
 80 
 81 
 82 
 83 // 4、es5里面的继承   对象冒充实现继承
 84 
 85 
 86     //    function Person(){
 87     //         this.name='张三';  /*属性*/
 88     //         this.age=20;
 89     //         this.run=function(){  /*实例方法*/
 90     //             alert(this.name+'在运动');
 91     //         }
 92 
 93     //     }      
 94     //     Person.prototype.sex="男";
 95     //     Person.prototype.work=function(){
 96     //          alert(this.name+'在工作');
 97 
 98     //     }
 99        
100     //     //Web类 继承Person类   使用原型链+对象冒充的组合继承模式
101 
102     //     function Web(){
103 
104     //         Person.call(this);    /*对象冒充实现继承*/
105     //     }
106 
107     //     var w=new Web();
108     //    // w.run();  //对象冒充可以继承构造函数里面的属性和方法
109 
110     //     w.work();  //对象冒充可以继承构造函数里面的属性和方法   但是没法继承原型链上面的属性和方法
111 
112 
113 
114 
115 // 5、es5里面的继承   原型链实现继承
116 
117     //    function Person(){
118     //         this.name='张三';  /*属性*/
119     //         this.age=20;
120     //         this.run=function(){  /*实例方法*/
121     //             alert(this.name+'在运动');
122     //         }
123 
124     //     }      
125     //     Person.prototype.sex="男";
126     //     Person.prototype.work=function(){
127     //          alert(this.name+'在工作');
128 
129     //     }
130        
131     //     //Web类 继承Person类   原型链+对象冒充的组合继承模式
132 
133     //     function Web(){
134          
135     //     }
136 
137     //    Web.prototype=new Person();   //原型链实现继承
138     //    var w=new Web();
139     //     //原型链实现继承:可以继承构造函数里面的属性和方法 也可以继承原型链上面的属性和方法
140     //     //w.run();
141 
142     //     w.work();
143 
144 
145 
146 
147 // 6、 原型链实现继承的 问题?
148 
149     //    function Person(name,age){
150     //         this.name=name;  /*属性*/
151     //         this.age=age;
152     //         this.run=function(){  /*实例方法*/
153     //             alert(this.name+'在运动');
154     //         }
155 
156     //     }      
157     //     Person.prototype.sex="男";
158     //     Person.prototype.work=function(){
159     //          alert(this.name+'在工作');
160 
161     //     }
162        
163     //    var p=new Person('李四',20);
164     //    p.run();
165 
166 
167 
168 
169 
170 
171     // function Person(name,age){
172     //         this.name=name;  /*属性*/
173     //         this.age=age;
174     //         this.run=function(){  /*实例方法*/
175     //             alert(this.name+'在运动');
176     //         }
177 
178     // }      
179     // Person.prototype.sex="男";
180     // Person.prototype.work=function(){
181     //         alert(this.name+'在工作');
182 
183     // }
184        
185       
186     // function Web(name,age){
187 
188         
189     // }
190 
191     // Web.prototype=new Person();
192 
193     // var w=new Web('赵四',20);   //实例化子类的时候没法给父类传参
194 
195     // w.run();   //undefined 在运动
196 
197     // // var w1=new Web('王五',22);
198 
199 
200 
201 
202 
203 
204 //7.原型链+对象冒充的组合继承模式
205 
206 
207 //   function Person(name,age){
208 //             this.name=name;  /*属性*/
209 //             this.age=age;
210 //             this.run=function(){  /*实例方法*/
211 //                 alert(this.name+'在运动');
212 //             }
213 
214 //     }      
215 //     Person.prototype.sex="男";
216 //     Person.prototype.work=function(){
217 //             alert(this.name+'在工作');
218 
219 //     }
220        
221       
222 //     function Web(name,age){
223 
224 //         Person.call(this,name,age);   //对象冒充继承   实例化子类可以给父类传参
225 //     }
226 
227 //     Web.prototype=new Person();
228 
229 //     var w=new Web('赵四',20);   //实例化子类的时候没法给父类传参
230 
231 //     // w.run();
232 //     w.work();
233 
234 //     // var w1=new Web('王五',22);
235 
236 
237 
238 
239 
240 
241 //8、原型链+对象冒充继承的另一种方式
242 
243 
244    function Person(name,age){
245             this.name=name;  /*属性*/
246             this.age=age;
247             this.run=function(){  /*实例方法*/
248                 alert(this.name+'在运动');
249             }
250 
251     }      
252     Person.prototype.sex="男";
253     Person.prototype.work=function(){
254             alert(this.name+'在工作');
255 
256     }
257        
258       
259     function Web(name,age){
260 
261         Person.call(this,name,age);   //对象冒充继承  可以继承构造函数里面的属性和方法、实例化子类可以给父类传参
262     }
263 
264     Web.prototype=Person.prototype;
265 
266     var w=new Web('赵四',20);   //实例化子类的时候没法给父类传参
267 
268      w.run();
269     // w.work();
270 
271     // var w1=new Web('王五',22);
272 
273 
274 
275 
276 
277 
278 
279         </script>
280 
281     </head>
282     <body>
283      
284     </body>
285 </html>

 

转载于:https://www.cnblogs.com/Spinoza/p/9364237.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值