-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextraConfigData.js
50 lines (47 loc) · 1.14 KB
/
extraConfigData.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
/*
* @Author: Tiny
* @Date: 2019-01-25 14:23:48
* @Last Modified by: [email protected]
* @Last Modified time: 2019-01-25 14:49:31
*/
/**
* 抽离配置数据
*/
function validdate (value) {
if (!value) {
alert('Invalid value')
location.href = '/error/invalid.php'
}
}
function toggleSelected (element) {
if (hasClass(element, 'selected')) {
removeClass(element, 'selected')
} else {
addClass(element, 'selected')
}
}
/**
* 上面代码的问题:
* 'Invalid value','/error/invalid.php','selected'是hardcoded,
* 如果validdate,toggleSelected这两个方法被多次调用,如果要改动这些数据,被调用多少次就要改多少次,
* 这时候极易出错
*/
let config = {
MSG_INVALID_VALUE: 'Invalid value',
URL_INVALID: '/error/invalid.php',
CSS_SELECTED: 'selected'
}
function validdate2 (value) {
if (!value) {
alert(config.MSG_INVALID_VALUE)
location.href = config.URL_INVALID
}
}
function toggleSelected2 (element) {
const SELECTED = config.CSS_SELECTED
if (hasClass(element, SELECTED)) {
removeClass(element, SELECTED)
} else {
addClass(element, SELECTED)
}
}