473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Do classes have to be global?

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.

Jun 26 '07 #1
1 1053
On Jun 27, 4:27 am, Christopher Nelson <cnel...@nycap.rr.comwrote:
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.
--
Rob

Jun 27 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Swartz | last post by:
Hi all. I'm working here on a small project of mine. I'm not new to programming, but I'm new to PHP. You have to understand that I'm coming from C++, OOP world, so my code might seems a little...
3
by: Logan K. | last post by:
What is a PHP class? I've seen a lot of talk about these and how they can help you out, but I've never seen a very good explanation. I've been working with PHP for just over 6 months and usually...
6
by: Jessica | last post by:
Hi, I have a question regarding the object-oriented issue. I understand that a class is a type. I have an array class. Now say that I want to implement an algorithm A that uses the array class....
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
3
by: NOSPM | last post by:
Don't know if this is the place to post this, but I will give it a go. :) I have a linklist class, and I have around 20 other classes that need to access the same info (the data) that is...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
18
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise....
4
by: Sheldon | last post by:
Hi, I have a series of classes that are all within the same file. Each is called at different times by the main script. Now I have discovered that I need several variables returned to the main...
11
by: Sylvia A. | last post by:
How can I define global classes in web application ? Classes can be set to session variables ? Thanks
3
by: r0g | last post by:
Hi There, I'm refactoring some old code that uses global variables and was originally written in one big flat file with a view to nicening it up and then extending it. The problem I have though...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.