C
Christopher Nelson
I have a working JavaScript class that I want to get out of the
global namespace. I have something like:
var foos;
function Foo(name) {
this.name = name;
foos[name] = this;
}
Foo.prototype.who = function() {
alert(this.name);
}
Bar.prototype = new Foo;
Bar.prototype.contructor = Bar;
function Bar(name,stuff) {
Foo.call(this,name);
this.stuff = stuff;
}
...
var aFoo = new Foo('bob');
var aBar = new Bar('bill',{a:'x', b:'y'});
which works fine but I want to make it myNamespace.Foo and
myNamespace.Bar. If at the start of the previous code I add something
like:
var myNamespace = new Object();
when add myNamespace to the front of each declaration like:
function myNamespace.Foo(name) {
this.name = name;
foos[name] = this;
}
then try to do
aBar.who();
I get an error saying that aBar() doesn't have a member who(). I've
tried 6 different things to fix that and am beginning to think that
objects must be at global scope. But I've spent hours Googling
JavaScript, class, and scope and can't find anything that says that.
On the other hand, I can't find any examples of non-global objects.
global namespace. I have something like:
var foos;
function Foo(name) {
this.name = name;
foos[name] = this;
}
Foo.prototype.who = function() {
alert(this.name);
}
Bar.prototype = new Foo;
Bar.prototype.contructor = Bar;
function Bar(name,stuff) {
Foo.call(this,name);
this.stuff = stuff;
}
...
var aFoo = new Foo('bob');
var aBar = new Bar('bill',{a:'x', b:'y'});
which works fine but I want to make it myNamespace.Foo and
myNamespace.Bar. If at the start of the previous code I add something
like:
var myNamespace = new Object();
when add myNamespace to the front of each declaration like:
function myNamespace.Foo(name) {
this.name = name;
foos[name] = this;
}
then try to do
aBar.who();
I get an error saying that aBar() doesn't have a member who(). I've
tried 6 different things to fix that and am beginning to think that
objects must be at global scope. But I've spent hours Googling
JavaScript, class, and scope and can't find anything that says that.
On the other hand, I can't find any examples of non-global objects.