BaseObject类
# 概述
本文主要介绍BaseObject
类,该类继承于Observable
类,一般情况下BaseObject
类也是其它类的父类,而很少直接当作构造函数进行实例化。
# 源码剖析
BaseObject
类实现如下:
class BaseObject extends Observable {
constructor(values) {
super();
this.on;
this.once;
this.un;
this.getUid(this);
this.values_ = null;
if (values !== undefined) {
this.setProperties(values);
}
}
get(key) {
let value;
if (this.values_ && this.values_.hasOwnProperty(key)) {
value = this.values_[key];
}
return value;
}
getKeys() {
return (this.values_ && Object.keys(this.values_)) || [];
}
getProperties() {
return (this.values_ && Object.assign({}, this.values_)) || {};
}
getPropertiesInternal() {
return this.values_;
}
hasProperties() {
return !!this.values_;
}
notify(key, oldValue) {
let eventType;
eventType = `change:${key}`;
if (this.hasListener(eventType)) {
this.dispatchEvent(new ObjectEvent(eventType, key, oldValue));
}
eventType = ObjectEventType.PROPERTYCHANGE;
if (this.hasListener(eventType)) {
this.dispatchEvent(new ObjectEvent(eventType, key, oldValue));
}
}
addChangeListener(key, listener) {
this.addEventListener(`change:${key}`, listener);
}
removeChangeListener(key, listener) {
this.removeEventListener(`change:${key}`, listener);
}
set(key, value, silent) {
const values = this.values_ || (this.values_ = {});
if (silent) {
values[key] = value;
} else {
const oldValue = values[key];
values[key] = value;
if (oldValue !== value) {
this.notify(key, oldValue);
}
}
}
setProperties(values, silent) {
for (const key in values) {
this.set(key, values[key], silent);
}
}
applyProperties(source) {
if (!source.values_) {
return;
}
Object.assign(this.values_ || (this.values_ = {}), source.values_);
}
unset(key, silent) {
if (this.values_ && key in this.values_) {
const oldValue = this.values_[key];
delete this.values_[key];
if (isEmpty(this.values_)) {
this.values_ = null;
}
if (!silent) {
this.notify(key, oldValue);
}
}
}
}
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
# BaseObject
类的主线
BaseObject
类构造函数接受一个对象参数values
,会调用 getUid
方法,该方法就是设置ol_uid
的值,初始化this.values_
为null
,然后判断参数values
是否存在,若存在,则调用this.setProperties
方法。
setProperties
方法就是通过for...in
遍历values
去调用set
方法。而set
方法中会去将值绑定到this.values_
上,还会进行this.values_[key]
的新旧值对比,若二者不等,则会调用this.notify
方法,第一次调用时该方法一定会触发;
notify
方法会去调用this.hasListener
判断该类型的事件是否注册过,若注册过,则调用this.dispatchEvent
去派发事件;然后会判断propertychange
类型的事件是否被注册过,若注册过,则调用this.dispatchEvent
派发事件。
# BaseObject
类的其它方法
get
方法
get
方法就是通过key
值获取this.values_[key]
的值
getKeys
方法
getKeys
方法获取this.values_
的key
值集合,可能是一个空数组[]
getProperties
方法
getProperties
方法就是获取this.values
方法,可能是一个空对象{}
getPropertiesInternal
方法
内部方法,作用同上
hasProperties
方法
hasProperties
方法用于判断this.values_
值是否存在
addChangeListener
和removeChangeListener
方法
addChangeListener
和removeChangeListener
方法就是包装了下type
,加了前缀change:
,然后调用对应的addEventListener
和removeEventListener
方法
applyProperties
方法
applyProperties
方法就是用来设置this.values_
,但是避免了触发事件
unset
方法
unset
方法用于清除this.values
某个键值对,若silent
参数为false
,则调用notify
方法用于通知相关监听事件
# 总结
本文介绍了BaseObject
类的主线流程以及相关方法。但是在实际应用中一般是先调用addChangeListener
注册事件,然后调用setProperties
方法设置属性(值),然后触发已经注册过的回调函数。