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 6218451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:36:42+00:00 2026-05-24T07:36:42+00:00

Here is my code structure, that I saw from someone else and am trying

  • 0

Here is my code structure, that I saw from someone else and am trying to make sense of it. I believe its a closure style and not a plugin style? maybe someone can clarify this for me if its a closure style.

Kind of plugin style. Here are some general questions.

  1. So the variable NP is in the global namespace correct? and just used
    as a reference to the closure and its inner public methods.
  2. Using NP (which is the namespace ) before a method is just a way to
    reference that method from outside the closure.
  3. If I set NP to null does it destroy and allow for garbage collection
    of everything inside of NP including all variables.

This is the bigger question here so I’ll put it out on its own.
When I trigger an event from an inner method actionMethod()
I want to be able to receive that event, from lets say another js file. For example home.js

home.js

    $(function(){

      NP.bind("CHANGED", event_handler);

     function event_handler(evt, data ){
       // do something....
     }

     $('#button').click(function(){
      // call my public method in my NP plugin
      NP.PublicMethod();
     });

  });

Closure ( style )

var NP = function(namespace){

     var innerVariable = "the outside scope can't here me";

     finction innerMethod(){
        //does some inner functionality to the closure
     }  

     namespace.PublicMethod = function(){
       // does some calling of inner function.
      // user can access this method from the myplugin directly.
      // example: NP.PublicMethod();
     }

     function actionMethod(){
       // trigger an event so others outside this closure can react
       namespace.trigger("CHANGED");
     }

    }(NP || {});

Please if someone can answer the first 3 questions then the larger question about the triggering of events. Thanks in advance

Update:

Something that ShankarSangoli made an example of by triggering the event from a DOM element.
The issue here is I have a dom element that is acting as a close button inside my closer, but home.js has no idea of it or no reference to, nor should it. So I need a way for to listen on the NP object level for events fired from it. As a test I tried the below code and it worked. No idea why as its not a DOM element, but in most languages you can fire an event from most generic objects. And in this case NP is in fact an object, or am I going mad in thinking its an object.

closure Object

   function actionMethod(){
           // trigger an event so others outside this closure can react
           $(namespace).trigger("CHANGED");
         }

home.js

$(TWCCP).bind("CLOSED_POPUP", onClosePopup);

Now I’m very confused about this. How is this possible now???

  • 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-05-24T07:36:43+00:00Added an answer on May 24, 2026 at 7:36 am
    1. Yes, NP is in global namespace.
    2. Yes, in order to use any method of NP you have to say NP.methodName();
    3. If set NP = null then it will be destroyed and javascript will take care of garbage collection and destroying it inner variable.

    You cannot use NP.bind unless NP is a jquery object associated to a dom element.

    The way to trigger any event is as below

    $("domElementSelector").trigger("click");//Or any otherr event, even custom event like in your case CHANGED can be triggered.
    

    For your last part of question try this

    Closure(style)

    var NP = function(namespace){
    
         var innerVariable = "the outside scope can't here me";
    
         finction innerMethod(){
            //does some inner functionality to the closure
         }  
    
         namespace.PublicMethod = function(){
           // does some calling of inner function.
          // user can access this method from the myplugin directly.
          // example: NP.PublicMethod();
         }
    
         function actionMethod(){
           // trigger an event so others outside this closure can react
           $(namespace).trigger("CHANGED");
         }
    
         namespace.bind = function(eventType, handler)
         {
            $(namespace).bind(eventType, handler);
         }
    return namespace;
    
        }(NP || {});
    

    home.js

    $(function(){
    
          NP.bind("CHANGED", event_handler);
    
         function event_handler(evt, data ){
           // do something....
         }
    
         $('#button').click(function(){
          // call my public method in my NP plugin
          NP.PublicMethod();
         });
    
      });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some old C code here that's built with nmake. Is there an
I have a piece of code here that i really could use some help
I am a total Groovy newbie. I saw the following code here . def
const static int foo = 42; I saw this in some code here on
I'm trying to make a function template that will accept two (or more) of
This is beyond both making sense and my control. That being said here is
I am trying to decide the best way to structure some code. I will
Edit: The code here still has some bugs in it, and it could do
See here: http://code.google.com/p/ie7-js/ Does anyone have any experience or remarks about this javascript? Is
I have this code here: var infiltrationResult; while(thisOption) { var trNode = document.createElement('tr'); var

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.