user562688
user562688

Reputation:

how to get current Function object reference

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 the doDrawFunc.

Upvotes: 21

Views: 21196

Answers (2)

PleaseStand
PleaseStand

Reputation: 32082

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.

Upvotes: 22

Ryan McGrath
Ryan McGrath

Reputation: 2042

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.

Upvotes: 0

Related Questions