W***o 发帖数: 6519 | 1 比如
var game = {
shoot: function() {
console.log("shooting now");
}
};
spy(game.shoot)
game.shoot();
....
game.shoot();
game.shoot(); |
p*****2 发帖数: 21240 | 2
加个counter行吗?
【在 W***o 的大作中提到】 : 比如 : var game = { : shoot: function() { : console.log("shooting now"); : } : }; : spy(game.shoot) : game.shoot(); : .... : game.shoot();
|
l**********n 发帖数: 8443 | 3 some kind of interceptor or decorator |
c*****n 发帖数: 173 | 4 var foo = function(){};
console.count(foo);
【在 W***o 的大作中提到】 : 比如 : var game = { : shoot: function() { : console.log("shooting now"); : } : }; : spy(game.shoot) : game.shoot(); : .... : game.shoot();
|
W***o 发帖数: 6519 | 5 不能改动原function
【在 p*****2 的大作中提到】 : : 加个counter行吗?
|
s*i 发帖数: 5025 | 6 是 浏览器端运行还是 Node?
[发表自未名空间手机版 - m.mitbbs.com] |
c*********e 发帖数: 16335 | 7 add a listener to a "click" event.
【在 W***o 的大作中提到】 : 比如 : var game = { : shoot: function() { : console.log("shooting now"); : } : }; : spy(game.shoot) : game.shoot(); : .... : game.shoot();
|
W***o 发帖数: 6519 | 8 there's no click event associated to the functional call.
【在 c*********e 的大作中提到】 : add a listener to a "click" event.
|
c*********e 发帖数: 16335 | 9 自己造一个button,这个button有个click event, 里面有game.shoot();
【在 W***o 的大作中提到】 : there's no click event associated to the functional call.
|
s*i 发帖数: 5025 | 10 ...
var spy = function(target){
var swap = target;
var count = 0;
return function(){
swap.apply(null, arguments);
count++;
console.log(count);
};
};
game.shoot = spy(game.shoot);
...
如果是在浏览器端运行的话,可以考虑各种 watch functions |
|
|
c*********e 发帖数: 16335 | 11 var spy = function(target){
var count = 0;
target.apply(null, arguments);
count++;
console.log(count);
};
【在 s*i 的大作中提到】 : ... : var spy = function(target){ : var swap = target; : var count = 0; : return function(){ : swap.apply(null, arguments); : count++; : console.log(count); : }; : };
|
k****i 发帖数: 101 | 12 require! meld
game = shoot: !->
console.log shooting
(! ->
counter = 0
<-! meld.after game, shoot
console.log shot ++counter
)!
game.shoot!
game.shoot!
【在 W***o 的大作中提到】 : 比如 : var game = { : shoot: function() { : console.log("shooting now"); : } : }; : spy(game.shoot) : game.shoot(); : .... : game.shoot();
|
W***o 发帖数: 6519 | 13 谢谢,不过game.shoot 不能assigned by spy(), 也就是说不能被修改;如果允许修改
,那就简单太多了
【在 s*i 的大作中提到】 : ... : var spy = function(target){ : var swap = target; : var count = 0; : return function(){ : swap.apply(null, arguments); : count++; : console.log(count); : }; : };
|
p*****2 发帖数: 21240 | 14
能不能把shoot replace掉,用另外一个函数,新函数call老函数?再加一个counter。
【在 W***o 的大作中提到】 : 谢谢,不过game.shoot 不能assigned by spy(), 也就是说不能被修改;如果允许修改 : ,那就简单太多了
|
n*****t 发帖数: 22014 | 15 node 吗?实在不行翻翻 v8 source code,不知道 node --debug 有没有用
【在 W***o 的大作中提到】 : 谢谢,不过game.shoot 不能assigned by spy(), 也就是说不能被修改;如果允许修改 : ,那就简单太多了
|
d****n 发帖数: 1637 | 16 var oldshoot = shoot;
var cnt =0;
shoot = function (opts){
cnt ++;
oldshoot(opts)
}
//load this after original shoot defined
【在 W***o 的大作中提到】 : 谢谢,不过game.shoot 不能assigned by spy(), 也就是说不能被修改;如果允许修改 : ,那就简单太多了
|
W***o 发帖数: 6519 | 17 感觉jasmine 里的spyOn 应该是用了,一会去看看jasmine的source code
【在 n*****t 的大作中提到】 : node 吗?实在不行翻翻 v8 source code,不知道 node --debug 有没有用
|
N*****m 发帖数: 42603 | 18 如果是node.js,webstorm可以profile
【在 W***o 的大作中提到】 : 比如 : var game = { : shoot: function() { : console.log("shooting now"); : } : }; : spy(game.shoot) : game.shoot(); : .... : game.shoot();
|
p*****2 发帖数: 21240 | 19 我也这个意思
【在 d****n 的大作中提到】 : var oldshoot = shoot; : var cnt =0; : shoot = function (opts){ : cnt ++; : oldshoot(opts) : } : //load this after original shoot defined
|
d****n 发帖数: 1637 | 20 var origCall = Function.prototype.call;
Function.prototype.call = function (thisArg) {
console.log("calling a function");
var args = Array.prototype.slice.call(arguments, 1);
origCall.apply(thisArg, args);
};
js自带的魔法, 得益于global object design
var origCall = Function.prototype.call;
Function.prototype.call = function (thisArg) {
console.log("calling a function");
var args = Array.prototype.slice.call(arguments, 1);
origCall.apply(thisArg, args);
};
http://stackoverflow.com/questions/5226550/can-i-override-the-j |
m******2 发帖数: 564 | 21 我也是这么想的
【在 p*****2 的大作中提到】 : 我也这个意思
|
n*****t 发帖数: 22014 | 22 shoot 里用到 this 就麻烦了吧
【在 s*i 的大作中提到】 : ... : var spy = function(target){ : var swap = target; : var count = 0; : return function(){ : swap.apply(null, arguments); : count++; : console.log(count); : }; : };
|