-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.js
36 lines (34 loc) · 895 Bytes
/
12.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
function Spy(target, method) {
return (function(){
var ret = Object.create(null);
ret.count = 0;
ret.method = target[method];
target[method] = function(){
ret.count++;
return ret.method;
}
return ret;
})()
}
module.exports = Spy
// standard answer
// an IEFF here actually did nothing so it can be delete
//function Spy(target, method) {
// var originalFunction = target[method]
//
// // use an object so we can pass by reference, not value
// // i.e. we can return result, but update count from this scope
// var result = {
// count: 0
// }
//
// // replace method with spy method
// target[method] = function() {
// result.count++ // track function was called
// return originalFunction.apply(this, arguments) // invoke original function
// }
//
// return result
//}
//
//module.exports = Spy