如何获取当前的Function对象引用

时间:2021-08-28 16:53:25
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.

我希望能够从doDrawFunc中访问userArgsLength。

2 个解决方案

#1


22  

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

您正在寻找arguments.callee.caller.userArgsLength。

  • arguments.callee is a reference to doDrawFunc.
  • arguments.callee是对doDrawFunc的引用。
  • .caller is the function that called it (handler).
  • .caller是调用它的函数(处理程序)。
  • .userArgsLength is the property of that function object.
  • .userArgsLength是该函数对象的属性。

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.

编辑:我不相信有任何方法可以避免arguments.callee而不更改您的主要功能。无论如何,您可能应该将回调需要访问的任何内容作为参数传递给该回调函数。您甚至可以将处理程序作为参数传递。

#2


0  

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.

将handler.userArgsLength的赋值移动到更早的点,将其推送到apply数组堆栈,然后bam,你可以认为它是最后的参数。

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.

不知道你为什么要使用arguments.callee;根据我的理解,如果你不小心,那么向后穿越会变得很慢。

#1


22  

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

您正在寻找arguments.callee.caller.userArgsLength。

  • arguments.callee is a reference to doDrawFunc.
  • arguments.callee是对doDrawFunc的引用。
  • .caller is the function that called it (handler).
  • .caller是调用它的函数(处理程序)。
  • .userArgsLength is the property of that function object.
  • .userArgsLength是该函数对象的属性。

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.

编辑:我不相信有任何方法可以避免arguments.callee而不更改您的主要功能。无论如何,您可能应该将回调需要访问的任何内容作为参数传递给该回调函数。您甚至可以将处理程序作为参数传递。

#2


0  

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.

将handler.userArgsLength的赋值移动到更早的点,将其推送到apply数组堆栈,然后bam,你可以认为它是最后的参数。

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.

不知道你为什么要使用arguments.callee;根据我的理解,如果你不小心,那么向后穿越会变得很慢。