由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - js: 怎么来监听某个function被call的次数?
相关主题
python decorator 调用问题Calling order of functions as arguments
请教python里面function的arguments的格式问题java小问题
python: how to import a decorator?function declaration
static如何作为函数?c的函数指针能不能弄得像matlab的function handle一样?
A simple question on Flexpython 问题,急,在线等。
gdb里怎么call member functionjava 8 也可以fp啊
Default function template arguments如何定义 Javascript overload function ?
ajax小问题javascript最让人崩溃的一点
相关话题的讨论汇总
话题: var话题: function话题: shoot话题: thisarg话题: spy
进入Programming版参与讨论
1 (共1页)
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
相关主题
gdb里怎么call member functionCalling order of functions as arguments
Default function template argumentsjava小问题
ajax小问题function declaration
进入Programming版参与讨论
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);
: };
: };

1 (共1页)
进入Programming版参与讨论
相关主题
javascript最让人崩溃的一点A simple question on Flex
how to proof (转载)gdb里怎么call member function
如果python command line positional arguments 里有些是运算,那这种argument 该怎么处理?Default function template arguments
谁在实际工作中写过c#或Java的custom attribute?ajax小问题
python decorator 调用问题Calling order of functions as arguments
请教python里面function的arguments的格式问题java小问题
python: how to import a decorator?function declaration
static如何作为函数?c的函数指针能不能弄得像matlab的function handle一样?
相关话题的讨论汇总
话题: var话题: function话题: shoot话题: thisarg话题: spy