继承练习2

 1     /*********对象冒充**********/
 2     function Parent(username){
 3         this.username=username;
 4         this.sayHello=function(){
 5             alert(this.username);
 6         }
 7     }
 8     function Child(username,password){
 9         this.method=Parent;
10         this.method(username);
11         delete this.method;
12         this.password=password;
13         this.sayWorld=function(){
14             alert(this.password);
15         }
16     }
17     var parent1 = new Parent("jack");
18     var child1 = new Child("kyle","123");
19     parent1.sayHello();
20     child1.sayHello();
21     child1.sayWorld();
22     /*********对象冒充**********/
23     /********call方法**********/
24     function Parent(username){
25         this.username=username;
26         this.sayHello=function(){
27             alert(this.username);
28         }
29     }
30     function Child(username,password){
31         Parent.call(this,username);
32         this.password=password;
33         this.sayWorld=function(){
34             alert(this.password);
35         }
36     }
37     var p1=new Parent("jack");
38     var c1=new Child("kyle","123");
39     p1.sayHello();
40     c1.sayHello();
41     c1.sayWorld();
42     /********call方法**********/
43     /********apply方法********/
44     function Parent(username){
45         this.username=username;
46         this.sayHello=function(){
47             alert(this.username);
48         }
49     }
50     function Child(username,password){
51         Parent.apply(this,new Array(username));
52         this.password=password;
53         this.sayWorld=function(){
54             alert(this.password);
55         }
56     }
57     var p1=new Parent("jack");
58     var c1=new Child("kyle","123");
59     p1.sayHello();
60     c1.sayHello();
61     c1.sayWorld();
62     /********apply方法********/
63     /********原型链**********/
64     function Parent(){}
65     Parent.prototype.hello="hello";
66     Parent.prototype.sayHello=function(){
67         alert(this.hello);
68     }
69     function Child(){}
70     Child.prototype = new Parent();
71     Child.prototype.world = "world";
72     Child.prototype.sayWorld = function(){
73         alert(this.world);
74     }
75     var c1 = new Child();
76     c1.sayHello();
77     c1.sayWorld();
78     /********原型链**********/
79     function Parent(hello){
80         this.hello=hello;
81         this.sayHello=function(){
82             alert(this.hello);    
83         }
84     }
85     function Child(hello,world){
86         Parent.call(this,hello);
87         this.world=world;
88         this.sayWorld=function(){
89             alert(this.world);
90         }
91     }
92     Child.prototype=new Parent();
93     Child.prototype.constructor=Child;
94     var c1=new Child("hello","world");
95     c1.sayHello();
96     c1.sayWorld();
posted @ 2012-07-09 09:22  kyiku  阅读(150)  评论(0编辑  收藏  举报