2

I have created a Javascript namespace like so:

var MyApp = function() {
      return {
        someFunction : function(x) {}
             alert(MyApp.y);
      }
}();

This allows me to do this:

MyApp.someFunction(x);

I would like to be able to do the following:

MyApp.y = "test" so that my someFunction can access this variable Y.

Any ideas on how to solve this? I'd like to keep my NS syntax intact so a solution that works with my example code would be nice.

Jorre
  • 17,273
  • 32
  • 100
  • 145
  • Is there any particular reason you're putting someFunction in an enclosure? You can't pass in new variables to that enclosure once you've made it as far as I know, but you could access global variables of course. If you don't *need* the enclosure however you can do `this.someFunction = function(x) ...` instead of the `return` block. Also at a guess you probably want to `new` that function when you're storing it in `MyApp`. – Vala Dec 15 '11 at 13:46

2 Answers2

3

What you described should work. (Except you have a syntax error and an unused argument x.)

var MyApp = function() {
  return {
    someFunction : function(x) {
         alert(MyApp.y);
    }
  }
}();
MyApp.y = 'test';
MyApp.someFunction() // alerts 'test'

Another approach is to treat your outer function more like a constructor and pass y as a closure:

var MyApp = (function (y) {
    return {
        y: y,
        someFunction: function () {
            alert(MyApp.y); // or alert(this.y)
        }
    }
} ('test'));
MyApp.someFunction(); // alerts 'test'
kojiro
  • 74,557
  • 19
  • 143
  • 201
3

I would go with something like this:

var MyApp = function() {
    var _y = "default value";
    return {
        someFunction: function(x) {
            console.log(_y);
        },
        setY: function (y) {
            _y = y;
        }
    }
}();

This will mean that it is safe to call MyApp.someFunction() before assigning a value to y. It also means that the contents of the variable is maintained within the namespace's scope, e.g.

console.log(MyApp._y); // undefined

Here would be how to use it:

MyApp.someFunction(); // "default value"
MyApp.setY("new value");
MyApp.someFunction(); // "new value"
jabclab
  • 14,786
  • 5
  • 54
  • 51
  • @akellehe why? JavaScript isn't Python. Using an underscore won't (by itself) prevent the variable from showing up in a `for…in` loop or being true when he runs `MyApp.hasOwnProperty('_y')`. Private members in JavaScript are just variables of the constructor. They're only accessible by private and privileged methods. [Ref](http://javascript.crockford.com/private.html) I'm not against this approach, I just don't see the point of the underscore, and OP doesn't ask for the value to be private. – kojiro Dec 15 '11 at 14:07
  • 1
    The underscore makes the private variable much more readable. When you see it somewhere it's scope is immediately understood. It's very easy to tell apart from local variables in class/instance methods. – KeatsKelleher Dec 15 '11 at 16:58