3

I am in the process of making my own namespace in JavaScript...

(function(window){
    (function(){
        var myNamespace = {
            somePublicMethod: function(){
            },
            anotherPublicMethod: function(){
            }
        }

        return (window.myNamespace = window.my = myNamespace)
    }());
})(window);

I'm new to these kinds of advanced JavaScript techniques and i'm trying to figure out the best way to call public methods from within my namespace. It appears that within my public methods this is being set to myNamespace.

Should I call public methods like...

AnotherPublicMethod: function(){
   this.somePublicMethod()
}

or...

AnotherPublicMethod: function(){
   my.somePublicMethod();
}

is there any difference?

Derek Adair
  • 21,846
  • 31
  • 97
  • 134

1 Answers1

3

The way I see it, if you use this you're using a direct reference to the object, whereas if you use my, the interpreter would need to traverse the scope chain until it finds my as a property of window.

But there may be arguments the other way as well.

EDIT:

I should note that since this is determined by how the function is called, it would require that the Activation object be that object.

So this would work:

my.anotherPublicMethod();

But this would not:

var test = my.anotherPublicMethod;
test();

If that's a possibility, then you should use my, or some other direct reference to the object. You could reduce the scope chain traversal by maintaining a reference to the object. Your myNamespace variable should work.


A little off topic, but I'd also note that your code won't work the way it is.

This line:

return (window.myNamespace = window.my = myNamespace)

...doesn't have access to the myNamespace variable.

Perhaps you meant something more like this?

(function(window){
    window.myNamespace = window.my = (function(){
        var myNamespace = {
            somePublicMethod: function(){
            },
            anotherPublicMethod: function(){
            }
        }
        return myNamespace;
    }());
})(window);
user113716
  • 318,772
  • 63
  • 451
  • 440
  • ha - good call! not off topic at all, I made a mistake when i was stripping my namespace for this question. Edited to work =) – Derek Adair Jan 05 '11 at 19:48
  • I'm new to this whole using JavaScript instead of relying on jQuery thing... my brain hurts. – Derek Adair Jan 05 '11 at 19:50
  • @Derek: Took a look at your update. Keep in mind that the `return` value of the inner function invocation is wasted since it isn't being assigned to anything. So you could remove `return`. Actually, the inner function isn't really necessary at all, though perhaps it is in your actual code? – user113716 Jan 05 '11 at 19:52
  • @Derek: Regarding your last comment, I can relate. I've had many brain aching, cramping days over the last six months or so, but I'm so glad I've done it. – user113716 Jan 05 '11 at 19:54
  • I actually just attempted to replicate what the `jQuery` framework uses to make the `$` shortcut... your way makes more sense, although there is probably a reason `jresig` did it like that. – Derek Adair Jan 05 '11 at 20:00
  • me too! I've had a torrid love affair with jQuery for the last year and i'm biting the bullet and examining how it works and even making my own framework for my backend =D it's great. – Derek Adair Jan 05 '11 at 20:01
  • @Derek: I just did an update to the answer to give a little more info. With regard to jQuery, yes, there must be some specific reason why they needed to add the extra scope inside. Could be you'd need the same thing eventually. – user113716 Jan 05 '11 at 20:07
  • ok... i'm a bit confused now. could you perhaps elaborate a bit more on your edit? – Derek Adair Jan 05 '11 at 20:22
  • @Derek: The value of `this` needs to be assigned to a function execution. When you call *any* function without defining its namespace, then `window` is implied. So no matter *where* the `test` function originated, if you call it like `test()`, the value of `this` in the execution of `test` will be `window`. If you call it like `my.somePublicMethod`, then the value of `this` will be the javascript object that `my` references. If you assign that same function to a property of some unrelated object, and then you call it from that object, then `this` will reference that object. (continued...) – user113716 Jan 05 '11 at 20:29
  • Take this example: `var someOther = {}; someOther.someProperty = my.somePublicMethod; someOther.someProperty()` Here we created a new object with a property called `someProperty` and made that property reference the same function that `my.somePublicMethod` references. Now if you call `someOther.someProperty();`, inside the function, `this` will be a reference to the `someOther` object. That's what I mean when I say that `this` is defined by *how* the function is called. (continued...) – user113716 Jan 05 '11 at 20:33
  • The function effectively looks at the object *before* the `.`, and sets that object as the value of `this`. If there isn't a `.`, then `window` is used. Two exceptions are the `.call()` and `.apply()` methods, which are a way of executing a function, but *manually* setting the value of `this`. In that case, the object (if any) to the left of the `.` is ignored. So if I did `my.somePublicMethod.call( jQuery )`, the value of `this` in the function would be the `jQuery` object (assuming it exists). – user113716 Jan 05 '11 at 20:37
  • ooooh! makes sense now. gotcha. – Derek Adair Jan 05 '11 at 20:42