Do classes have to be global?

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

RobG

I have a working JavaScript class that I want to get out of the
global namespace. I have something like:

var foos;

I think you meant...

var foos = {};

function Foo(name) {
this.name = name;
foos[name] = this;

....otherwise you will get an error here.

}

Foo.prototype.who = function() {
alert(this.name);
}

Bar.prototype = new Foo;
Bar.prototype.contructor = Bar;

It's bad form (but not illegal) to set a prototype property before
decalaring the function it relates to.

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.

Stop thinking that... :)
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.

Try:

// Create or extend namespace
var x = x || {};

x.foos = {};

x.Foo = function (name) {
this.name = name;
x.foos[name] = this;
}

x.Foo.prototype.who = function() {
alert(this.name);
}

// When creating[1] functions this way, make sure you declare
// them before trying to set their properties.
x.Bar = function (name, stuff) {
x.Foo.call(this, name);
this.stuff = stuff;
}

x.Bar.prototype = new x.Foo;
x.Bar.prototype.contructor = x.Bar;

var aFoo = new x.Foo('bob');
aFoo.who();

var aBar = new x.Bar('bill',{a:'x', b:'y'});
aBar.who();



1. "this way" being a statement that creates an object property and
assigns it a value of a reference to a function object (the result of
the function expression on the r.h.s.). Clearly you need to do that
before you can assign a value to the function's prototype property.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,419
Latest member
ArturoBres

Latest Threads

Top