开发者

how to get current Function object reference

开发者 https://www.devze.com 2023-02-02 22:35 出处:网络
itself.bind = function (object, method, callback, context, args, includeEventArgs) { var handler, originalArgLen;
itself.bind = function (object, method, callback, context, args, includeEventArgs) {

  var handler, originalArgLen;

  args = args.slice(0, arguments.length);
  originalArgLen = args.length;
  context = context || null;

  handler = function () {
     if (includeEventArgs) {
        for (var i = arguments.length - 1; i >= 0; i--) {
           args.push(arguments[i]);
        }
     }

     callback.apply(context, args);
  };

  handler.userArgsLength = originalArgLength; 

  object[method] = handler;
};

Suppose I call

TOOL.bind(canvas, "onmouseover",  doDrawFunc, [currentDrawingTool], true); 

I want to be able to access userArgsLength from from within th开发者_运维技巧e doDrawFunc.


You are looking for arguments.callee.caller.userArgsLength.

  • arguments.callee is a reference to doDrawFunc.
  • .caller is the function that called it (handler).
  • .userArgsLength is the property of that function object.

Edit: I do not believe there is any way to avoid arguments.callee without changing your main function. You probably should be passing whatever the callback needs access as an argument to that callback function anyways. You could even pass in handler as an argument.


Move the assignment of handler.userArgsLength to an earlier point, shove it onto the apply array stack, and bam, you can assume it's the final argument.

Not sure why you'd wanna use arguments.callee anyway; from what I understand, traversing backwards like that can get really slow if you're not careful.

0

精彩评论

暂无评论...
验证码 换一张
取 消