Javascript对象学习2 - 继承

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title>Javascript的继承的简单学习</title>
</head>
<body>
       
    <script type="text/javascript">
       
        (function(){
           
            /**
             * Javascript的继承的简单学习
             *
             * 1.对象冒充方式
             */
/*        
            //被继承对象
             function User(name) {
                this.name = name;
                this.sayName = function() {
                    alert(this.name);
                }
             }
             function Student(name,age) {
                this.temp = User;
                this.temp(name);
                //删除对User的引用,确保后续代码不能再次通过temp操作User,要不this.temp("aa");还会起作用
                //delete this.temp;
                //this.temp("aa");
                   
                //新属性和新方法都必须在删除新方法的代码行后定义。否则,可能会覆盖父类相关的属性和方法
                //this.name = "bb";
                this.age = age;
                this.showAge = function() {
                    alert(this.age);
                }
             }
             //调用
             var stu1 = new Student("tom",23);
             stu1.sayName();
             stu1.showAge();
*/
            /**
             * 1.1.继承:对象冒充方式(多重继承)
             */
           
/*          //被继承对象1
             function User(name) {
                this.name = name;
                this.sayName = function() {
                    alert(this.name);
                }
             }
             //被继承对象2
             function Like(item) {
                this.item = item;
            //  this.name = "";
                this.play = function() {
                    alert(this.item);
                }
             }
             function Student(name,age,item) {
                this.user = User;
                this.user(name);
                delete this.user;
                this.like = Like;
                this.like(item);
                delete this.like;
                this.age = age;
                this.showAge = function() {
                    alert(this.age);
                }
             }
            //调用
             var stu1 = new Student("Rose",25,"basketball");
             stu1.sayName();
             stu1.showAge();
             stu1.play();
             //缺点:如果存在两个类User和Like具有同名的属性或者方法,Like具有更高的优先级
                
*/
            /**
             * 2.继承:使用call()方法
             */
/*          function show(age,item) {
                alert(this.name + " --- " + age + " --- " + item);
            }
            var obj = new Object();
            obj.name = "tom";
            //调用
            show.call(obj,21,"football"); //第一个参数最好不要少
            //相当于:obj.show = show(); → show(21,"football")
*/
            /**
             * 2.1 继承:call()方法继承,多重继承
             */
/*          //被继承对象1
             function User(name) {
                this.name = name;
                this.sayName = function() {
                    alert(this.name);
                }
             }
             //被继承对象2
             function Like(item) {
                this.item = item;
            //  this.name = "";
                this.play = function() {
                    alert(this.item);
                }
             }
             function Student(name,age,item) {
                User.call(this,name);
                Like.call(this,item);
                this.age = age;
                this.showAge = function() {
                    alert(this.age);
                }
             }
            //调用
             var stu1 = new Student("Rose",25,"basketball");
             stu1.sayName();
             stu1.showAge();
             stu1.play();
             alert(stu1 instanceof User);//false
             alert(stu1 instanceof Like);//false
             alert(stu1 instanceof Student);//true
             alert(stu1.constructor); //function Sudent()....
*/
            /**
             * 3.继承:apply()方法
             */
               
/*          //被继承对象1
             function User(name) {
                this.name = name;
                this.sayName = function() {
                    alert(this.name);
                }
             }
             //被继承对象2
             function Like(item) {
                this.item = item;
            //  this.name = "";
                this.play = function() {
                    alert(this.item);
                }
             }
             function Student(name,age,item) {
                User.apply(this,[name]);//变化之处
                Like.call(this,[item]);
                this.age = age;
                this.showAge = function() {
                    alert(this.age);
                }
             }
            //调用
             var stu1 = new Student("Rose",25,"basketball");
             stu1.sayName();
             stu1.showAge();
             stu1.play();
             alert(stu1 instanceof User);//false
             alert(stu1 instanceof Like);//false
             alert(stu1 instanceof Student);//true
             alert(stu1.constructor); //function Sudent()....
*/
            /**
             * 4.继承:原型连方式
             */
               
            //被继承对象
/*          function User() {}
            User.prototype.name = "";
            User.prototype.sayName = function() {
                alert(this.name);
            };
               
            function Student() {}
            //①将User中所有通过prototype设置的属性或方法放到Student中,并覆盖了Student中所有的prototype设置。所以应该放在第一行
            Student.prototype = new User();
            Student.prototype.age = 0;
            Student.prototype.showAge = function() {
                alert(this.age);
            }
            //Student.prototype = new User(); //一定要放在第一行哦
            var stu = new Student();
            stu.name = "tom";
            stu.age = 23;
            stu.sayName();
            stu.showAge();
             alert(stu instanceof User);//true
             alert(stu instanceof Student);//true
             alert(stu.constructor); //function User()....
            //缺点:不支持多重继承,构造函数不能有参数
*/
            /**
             * 5.继承:混合模式 = 构造函数 + 原型链方式
             */
/*           function User(name) {
                this.name = name;
             }
             User.prototype.sayName = function() {
                alert(this.name);
             }
             function Student(name,age) {
                //User.call(this,name);
                this.age = age;
             }
             //将User中所有通过prototype设置的方法放到Student中
             Student.prototype = new User();
             Student.prototype.showAge = function() {
                alert(this.age);
             }
             //调用
            var stu = new Student();
            stu.name = "tom";
            stu.age = 23;
            stu.sayName();
            stu.showAge();
             alert(stu instanceof User);//true
             alert(stu instanceof Student);//true
             alert(stu.constructor); //function User(name) {this.name = name;}
*/
             function User(name) {
                this.name = name;
             }
             User.prototype.sayName = function() {
                alert(this.name);
             }
             function Student(name,age) {
                //User.call(this,name);
                //this.age = age;
             }
             //将User中所有通过prototype设置的方法放到Student中
             Student.prototype = new User();
             Student.prototype.age = 0;
             Student.prototype.showAge = function() {
                alert(this.age);
             }
             //调用
            var stu = new Student();
            stu.name = "tom";
            stu.age = 23;
            stu.sayName();
            stu.showAge();
             alert(stu instanceof User);//true
             alert(stu instanceof Student);//true
             alert(stu.constructor); //function User(name) {this.name = name;}
        })();
       
    </script>
</body>
</html>


上一篇:电脑故障由于其配置信息(注册表中的)不完整或已损坏,Windows 无法启动这个硬件设备. (代码 19)


下一篇:在虚拟机安装OpenSuse Tumbleweed