forked from lfthwjx/effective-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.js
52 lines (49 loc) · 1.31 KB
/
demo.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
// 定义一个接受参数的选项对象的函数
function Alert(obj) {
this.level = obj.level;
this.msg = obj.msg;
}
var ale = new Alert({
level: 0,
msg: 'hello'
});
console.log(ale); // Alert { level: 0, msg: 'hello' }
// 当然如果一些参数是必选的话,我们把他们单独拿出来,而且参数的选项对象上的属性不是必选的
function Alert1(level, msg, options) {
this.level = level;
this.msg = msg;
for(var p in options) {
this[p] = options[p]
}
}
var ale1 = new Alert1(1, 'find error', {
count: 8,
theme: 'default'
});
console.log(ale1); // Alert1 { level: 1, msg: 'find error', count: 8, theme: 'default' }
// 使用extend函数扩展我们的参数对象
function extend(target, source) {
if(source) {
for(var p in source) {
var val = source[p];
if('undefined' !== typeof val) {
target[p] = val;
}
}
}
return target;
}
// 升级原来的构造函数
function Alert2(level, msg, options) {
var opt = extend({
level: level,
msg: msg
});
opt = extend(opt, options);
extend(this, opt);
}
var ale2 = new Alert2(2, 'bug', {
count: 1,
theme: 'highlight'
});
console.log(ale2); // Alert2 { level: 2, msg: 'bug', count: 1, theme: 'highlight' }