The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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.

Javascript doesn't actually have classes. It has objects and it has functions. Constructors are just normal functions -- It's how they are invoked, which makes them a constructor.

Yes, you can namespace constructors, just like any other function. In Javascript, you can declare functions in two ways; Either you use the static syntax:

Code:

function foo() {
}

Or you use the dynamic syntax:

Code:

foo = function() {
}

Both accomplish the same, but the first is resolved at parse-time, while the latter is resolved at run-time. Because of this, you can't use an expression as name, with the static syntax, since the name can only be resolved at run-time. It's perfectly valid to declare a constructor using dynamic syntax though. There is no real benefit of using static syntax; It's mainly in the language to make it easier to use the language for programmers coming from C-like languages, where the dynamic syntax may seem strange.

I recommend that you simply don't use static syntax at all. It makes your code more uniform, if you use just one syntax, and it also shows better what is really happening behind the scenes in the Javascript language.

Note that you need to declare Bar before you can assign to its prototype property. This is because functions are regular variables in Javascript, so before you can assign to it, it needs to be declared. If you use static syntax, the chronology isn't important, since it's interpreted at parse-time.

You had a few bugs in the code as well, which may have prevented it from working. If you aren't already, you should get firefox + firebug. If you select the tab 'Console' and enable the large command line, you can type out small program-snippets, and have them evaluated interactively. It's priceless for debugging Javascript.

But I need it to work below global scope

Thanks but my original post included re-typed code that worked in the global scope; I'm not too surprised if what I actually posted was buggy. My problem was in trying to move it down into another object. Simply prefixing ns. to all the relevant lines fails and I don't see why. Bar ends up without Foo's prototype functions.

Thanks but my original post included re-typed code that worked in the global scope; I'm not too surprised if what I actually posted was buggy. My problem was in trying to move it down into another object. Simply prefixing ns. to all the relevant lines fails and I don't see why. Bar ends up without Foo's prototype functions.