简体   繁体   English

从事件处理程序闭包访问公共和私有方法

[英]Access to public and private methods from event handler closure

I have a difficulty in understanding, how my current JavaScript code works.我很难理解我当前的 JavaScript 代码是如何工作的。 I've managed to solve a problem in accessing private object method from event handler closure, but I'd like to know why does it work so.我已经设法解决了从事件处理程序闭包访问私有对象方法的问题,但我想知道为什么它会这样工作。

The code utilizes the well-known module/plugin metaphor:该代码使用了众所周知的模块/插件隐喻:

(function(module, $, undefined)
{
  function myPrivateCode(e){ /*...*/ }

  module.myPublicCode = function(e) { /*...*/ }

  module.init = function()
  {
    var that = this;
    $('.clickable').click(function(e)
    {
      if($(e.target).hasClass('classX'))
      {
         that.myPublicCode(e.target); // requires 'that' to work
      }
      else
      {
         // that.
         myPrivateCode(e.target);     // will fail if 'that' uncommented
      }
    });
  }

}(window.module = window.module || {}, jQuery ));

In the code I set a click handler which invokes either public or private method.在代码中,我设置了一个调用公共或私有方法的点击处理程序。 It's perfectly conceivable that we need to pass an object reference into the event handler closure, which is done by that local variable.完全可以想象,我们需要将对象引用传递到事件处理程序闭包中,这是由that局部变量完成的。 What is strange to me is that myPrivateCode does neither require that as a refernce, nor fails due to its "privacy".令我感到奇怪的是myPrivateCode既不需要that作为参考,也不会因为它的“隐私”而失败。 This makes me think that myPrivateCode accesses not the appropriate object, and works somehow differently to expected way.这让我认为myPrivateCode访问的不是适当的对象,并且以某种方式与预期的方式不同。 Could someone explain what happens?有人可以解释发生了什么吗? Certainly I'm missing something.当然我错过了一些东西。

Your call to myPrivateCode(e.target);您对myPrivateCode(e.target); is running in the context of the anonymous function that you pass as a handler to the click function.在您作为处理程序传递给click函数的匿名函数的上下文中运行。

For more information, read up on closures .有关更多信息,请阅读闭包

For a simpler example, try out this code:举一个更简单的例子,试试这段代码:

var foo = function () {
    var a = 1;

    return function (b) {
        return a+b;
    }
};

var bar = foo();

bar(1); // 2

bar(1) will always always gives 2, because a = 1 was in scope when the function was created. bar(1)将始终给出 2,因为创建函数时a = 1在范围内。 In your case, a is your that and your handler is the closed function.在你的情况下, a是你的that ,你的处理程序是关闭的函数。

http://jsfiddle.net/Fh8d3/ http://jsfiddle.net/Fh8d3/

Both that and myPrivateCode are available to your event handler through a closure . thatmyPrivateCode都可以通过闭包供您的事件处理程序使用。 In short, what's going on is that every variable and function you declare inside of another function has access to the outer scope.简而言之,发生的事情是您在另一个函数内部声明的每个变量和函数都可以访问外部范围。

myPublicCode , on the other hand, is not available through closures, because it's being assigned to your module object specifically.另一方面, myPublicCode不能通过闭包使用,因为它被专门分配给您的module对象。 So the only way to call it is by using module.myPublicCode() (or that.myPublicCode() as you did – but you don't actually need that there, since module is also available).因此,调用它的唯一方法是使用module.myPublicCode() that.myPublicCode()或像您所做的那样使用 that.myPublicCode() - 但您实际上并不需要that ,因为module也可用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM