-
Notifications
You must be signed in to change notification settings - Fork 0
/
js实现多继承.js
62 lines (61 loc) · 1.17 KB
/
js实现多继承.js
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
function M1(){
this.hello = 'hello'
console.log('m1')
}
function M2(){
this.world = 'world'
console.log('m2')
}
function S(){
M1.call(this)
M2.call(this)
}
S.prototype = Object.create(M1.prototype)
console.log(S.prototype)
Object.assign(S.prototype,M2.prototype)
S.prototype.constructor = S;
var s = new S()
console.log(s)
//----------------------------------------------
//js的几种继承方式,也写在这里
//构造函数
function Parent(age){
this.name = 'mike'
this.age = age
}
function Child(age){
Parent.call(this,age)
}
var test = new Child(21)
//原型链继承
function Parent(){
this.name = 'mike'
}
function Child(){
this.age = 12
}
Child.prototype = new Parent()
//组合继承
function Parent(age){
this.name = 'mike'
this.age = age
}
Parent.prototype.run = function(){
return this.name + '666' +this.age
}
function Child(age){
Parent.call(this,age)
}
Child.prototype = new Parent()
var test = new Child(21)
//原型式继承
function obj(o){
function F(){}
F.prototype = o
return new F()
}
var box = {
name:'box1',
arr:[1,2]
}
var b1 = obj(box)