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.

1
  • 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. Commented Dec 15, 2011 at 13:46

2 Answers 2

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'
Sign up to request clarification or add additional context in comments.

1 Comment

you're right, it does. Seems like I missed something minor in my code. Thanks!
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"

2 Comments

@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 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.
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.

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.