3

I have created a Javascript namespace to avoid conflict with other Javascript codes.

var ns = {
   init: function() {
      $('a').click(this.clickHandler);
   },
   clickHandler: function() {
      // Some code here ..

      // The keyword "this" does not reference my "ns" object anymore. 
      // Now, it represents the "anchor"
      this.updateUI();
   },
   updateUI: function() {
      // Some code here ...
   }
};

Please, how can I reference my enclosing namespace?

0

4 Answers 4

6

$.proxy

$('a').click($.proxy(this.clickHandler, this));
Sign up to request clarification or add additional context in comments.

Comments

4

You can bind event handler to an anonymous function and call clickHandler within it. This way the context will still refer to ns object.

var ns = {
   init: function() {
      var self = this; // store context in closure chain
      $('a').click(function () {
         self.clickHandler();
      });
   },
   clickHandler: function() {
      this.updateUI();
   },
   updateUI: function() {
      // Some code here ...
   }
};

3 Comments

How does 'this' get the right value inside the clickHandler function?
@LeeMeador, context points to dom object in dom objects' event handlers. In our anonymous event handler, the context points to dom element and we use closure variable to get original context. Finally, when clickHandler invokes, it will have the correct context as it is invoked by ns object.
I see. Still figuring out how the 'this'/context gets set. I tried it here in case anyone else wants to see it. jsfiddle.net/LMaBq
1

Here is an article: http://www.codeproject.com/Articles/108786/Encapsulation-in-JavaScript

It explains to create a closure in the namespace where you can store stuff (like the original 'this')

var ns = (function () {
    var self;

    return {
        init: function () {
            self = this;
            $('a').click(this.clickHandler);
        },
        clickHandler: function () {
            // Some code here ..
            self.updateUI();
        },
        updateUI: function () {
            // Some code here ...
        }
    };
})();

FIDDLE HERE

Comments

0

A good way to do that is to define a local variable in a function that refers to it. This helps when "this" changes on you. Your code could look something like this:

var ns = new (function() {
    var self = this;
    self.init = function() {
        $('a').click(self.clickHandler);
    },
    self.clickHandler = function() {
        // Some code here ..

        // The keyword "this" does not reference my "ns" object anymore. 
        // Now, it represents the "anchor"
        self.updateUI();
   },
   self.updateUI = function() {
      // Some code here ...
   }
})();

This allows you to still reference the event handler with this and then reference your namespace with the locally defined reference that is only available from within.

2 Comments

It can be fixed as shown here jsfiddle.net/JYAXL -- Only one change needed. The problem is that 'self' will get the context value (the value of the 'this') at the time when the object is created to be assigned to 'ns'.
You're right, my mistake. I was thinking of a function. I updated to fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.