##面向对象
- 面向对象就是将需要的功能封装为一个对象,这个对象称之为类,台湾翻译为面向物体编程,个人认为比较确切
- 面向对象的特征
- 封装(就是将私有变量方法封装,高内聚低耦合)
- 继承(子类继承父类),即一个类继承另一个类的属性和方法
- 多态(根据函数的参数等不一样的状态,构成不一样的方法)
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
let log = console.log.bind(console)
log('----------------------封装,主要是封装私有变量-----------------------')
let Book = function (id, name, price) {
/**
* 私有变量
* @type {number}
* @private
*/
let _num = 1
let _this = this
this.id = id
this.name = name
this.price = price
/**
* 私有方法
*/
function checkId () {
log(_this.id, _num, _this.price)
}
checkId()
// 公共方法
this.id = id
}
// 类静态共有属性
Book.face = 'paper'
// 静态方法
Book.fuck = function () {
log('fuck')
}
// 通过闭包实现封装
let Book2 = (function () {
// 通过闭包,将属性包起来
/**
* 静态变量
* @type {number}
* @private
*/
let _num = 2
return function (id, name) {
this.name = name
this.fuck = function () {
log(this.name, _num)
}
}
})()
let b = new Book2(1, 'test')
b.fuck()
log('-----------------------继承--------------------------')
log('-------类式继承, 引用类型容易和父级绑定到一起,导致问题--------')
~function () {
let SuperClass = function () {
this.superValue = true
this.books = [1, 2, 3, 4]
}
SuperClass.prototype.getSuperValue = function () {
return this.superValue
}
function SubClass () {
this.subValue = false
}
SubClass.prototype = new SuperClass()
SubClass.prototype.getSubValue = function () {
return this.subValue
}
SubClass.prototype.setBook = function () {
this.books.push('setBook')
}
let s = new SubClass()
let o = new SubClass()
log('subValue = ' + o.getSubValue(), 'superValue = ' + o.getSuperValue())
o.setBook()
s.books.push('jejejej')
log(o.books, s.books)
}()
log('------构造继承-----')
~function () {
function SuperClass (id) {
this.books = [1, 2, 3]
this.id = id
}
SuperClass.prototype.showBooks = function () {
log(this.books)
}
function SubClass (id) {
// 关键
SuperClass.call(this, id)
}
SubClass.prototype = new SuperClass()
let o1 = new SubClass(1)
let o2 = new SubClass(2)
o1.books.push('34')
o2.books.push('43')
o1.showBooks()
o2.showBooks()
}()
log('------原型继承-----')
~function () {
function inheritObject (o) {
function F () {
}
F.prototype = o
return new F()
}
function inheritPrototype (subClass, superClass) {
let p = inheritObject(superClass.prototype)
p.constructor = subClass
subClass.prototype = p
}
function SuperClass (name) {
this.name = name
this.colors = [1, 2, 2, 3]
}
SuperClass.prototype.getName = function () {
return this.name
}
function SubClass (name, time) {
// 会开辟内存,构造函数式继承
SuperClass.call(this, name)
this.time = time
}
inheritPrototype(SubClass, SuperClass)
SubClass.prototype.getTime = function () {
return this.time
}
let o1 = new SubClass('23', 3434)
let o2 = new SubClass('234', 34)
o1.colors.push('dsfdsf')
log(o1.colors, o2.colors)
o1.getName = function () {
log('324234324')
}
o1.getName()
log(o2.getName())
}()