I was wondering if there was a way to keep namespaces in scope within the functions in a Javascript object.
First off, let's set up some stuff. Assume that we have a class (well, as close to a class as Javascript gets) Foo in the namespace FooBar. Then in the global namespace (window), we have the class Bar, which instantiates of Foo, so...
FooBar.Foo = function() {
}
function Bar() {
this.init();
}
Bar.prototype = {
init: function() {
this.foo = new FooBar.Foo();
}
}
Currently I have two systems for bringing things into scope: 1. using("FooBar") and unusing("FooBar") these functions move references to the contents of a given namespace in and out of the global namespace 2. with(namespace("FooBar")) {} this uses normal with behavior, giving it an object containing references to everything contained in the namespace. So right now, I have to use one of these methods inside of every function to bring the namespace into scope. I'm trying to see if there's a method of defining these when the class is declared and having them still be in scope for just this class due to closure of some sort...
FooBar.Foo = function() {
}
using("FooBar");
function Bar() {
this.init();
}
Bar.prototype = {
init: function() {
this.foo = new Foo();
}
}
unusing("FooBar");
So, yeah, is something like this even possible, or am I stuck bringing these into scope really often?