Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8956949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:52:45+00:00 2026-06-15T14:52:45+00:00

I have a difficulty in understanding, how my current JavaScript code works. I’ve managed

  • 0

I have a difficulty in understanding, how my current JavaScript code works. 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. What is strange to me is that myPrivateCode does neither require that as a refernce, nor fails due to its "privacy". This makes me think that myPrivateCode accesses not the appropriate object, and works somehow differently to expected way. Could someone explain what happens? Certainly I’m missing something.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:52:47+00:00Added an answer on June 15, 2026 at 2:52 pm

    Both that and myPrivateCode are available to your event handler through a closure. 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. 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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a difficulty understanding getchar() . In the following program getchar works as
I have encountered a difficulty in understanding the subroutine syntax. using the following code:
I have difficulty understanding how a javascript callback should work. I am using the
Comming from Java, I have difficulty with the code below. In my understanding b
I have difficulty understanding this piece of code : Particularly this part: // check
I'm not a C++ programmer and have difficulty understanding the explanations given on websites.
i have a little difficulty in understanding how to do some INSERT SELECT. For
I'm having a little difficulty understanding alias_method / alias_method_chain . I have the following
I have some jQuery code that is having difficulty running because I'm using it
I have difficulty understanding SELECT * FROM myTable WHERE 0 = ... in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.