-
Notifications
You must be signed in to change notification settings - Fork 0
/
es6-test.js
63 lines (50 loc) · 1.05 KB
/
es6-test.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
63
/**
* Created by didi on 17/12/7.
*/
'use strict';
let obj = {
name: 'miracle'
};
let p1 = new Proxy(obj, {
get:function(target, property){
return target[property]+' proxy';
}
});
p1.sex = 'male';
console.log(p1);
//proxy实现链式写法
var funcs = {
sqrt: n=>n*n,
double: n=>2*n,
add: n=>n+2
}
var pipe = function(value) {
var evtStack = [];
var p = new Proxy({},{
get: function(target, property) {
if(property === 'get'){
return evtStack.reduce(function(val, fn){
return fn(val)
}, value);
}
evtStack.push(funcs[property]);
return p;
}
});
return p;
};
console.log(pipe(3).sqrt.double.add.get);
//多态
function Obama(){}
Obama.prototype.sayHello = function(){
console.log('hello');
}
function Xijinping(){};
Xijinping.prototype.sayHello = function(){
console.log('你好');
}
function sayHi(leader){
leader.sayHello();
}
sayHi(new Obama(),'多态');
sayHi(new Xijinping(), '多态');