473,218 Members | 1,397 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,218 software developers and data experts.

Conditional include of JS files -- how?

I've a set of Javascript classes that maintain state. For example,
gm.js might be:

var GroupManager { groups: {} };

Over time I add new groups to the list:

GroupManager.groups[key] = myGroup;

If I include another Javascript file that also includes a reference to
include gm.js, or do this through an Ajax call, this new loading of
gm.js overwrites my existing GroupManager variable and I lose all my
accumulated data.

So I thought I could do

if(!GroupManager) { GroupManager = { groups: {} }; }

But the GroupManager so instantiated lives only within the if()
block. And to include an initial var GroupManager simply kills state.

Are there any techniques for allowing multiple includes of a
Javascript file while detecting a previous include, allowing me to
preserve any existing variables?

Thanks,
Jerome
Aug 27 '08 #1
4 2201
rocketeer wrote:
I've a set of Javascript classes that maintain state.
There are probably no classes, you are likely using an implementation that
supports only prototype-based inheritance (ECMAScript 1 to 3-based). (And
no, this term does not have anything to do with the Prototype.js junk.)
If I include another Javascript file that also includes a reference to
include gm.js, or do this through an Ajax call, this new loading of
gm.js overwrites my existing GroupManager variable and I lose all my
accumulated data.

So I thought I could do

if(!GroupManager) { GroupManager = { groups: {} }; }

But the GroupManager so instantiated lives only within the if()
block.
You are mistaken. By default, ECMAScript implementations do not provide
block scoping, but lexical scoping. And since you did not declare
`GroupManager', without further information one has to assume the object
in the scope chain as which property it is accessed is the Global Object.
RTFM.
And to include an initial var GroupManager simply kills state.
Because variable instantiation comes before execution.
Are there any techniques for allowing multiple includes of a
Javascript file while detecting a previous include, allowing me to
preserve any existing variables?
The only possibility I know to work around that is

if (typeof x == "undefined")
{
eval('var x = "foo";');
}
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Aug 27 '08 #2
On Aug 28, 6:56*am, rocketeer <java...@sbcglobal.netwrote:
I've a set of Javascript classes that maintain state. *For example,
gm.js might be:

var GroupManager { groups: {} };

Over time I add new groups to the list:

GroupManager.groups[key] = myGroup;
One approach is to put a declaration at the start of the file in the
appropriate scope (global here) and conditionally assign it a value:

var groupManager = groupManager || {groups: {}};
So groupManager is a global variable referencing an object that holds
whatever (provided you are aware that any time groupManager evaluates
to a falsey value it will be assigned a reference to the object
literal on the right hand side of the || expression).

Declaring a variable with var can't alter the value of an existing
variable.

Oh, by convention only constructors or constants start with a capital
letter, hence groupManager (or GROUPMANAGER for constants, but I don't
use that). :-)

If I include another Javascript file that also includes a reference to
include gm.js, or do this through an Ajax call, this new loading of
gm.js overwrites my existing GroupManager variable and I lose all my
accumulated data.

So I thought I could do

if(!GroupManager) { *GroupManager = { groups: {} }; }
If GroupManager has not be declared or initialised some other way,
that is a syntax error. If you include var to declare GroupManager,
it is essentially the same as the suggestion above does, i.e.

if (!groupManager) { var groupManager = { groups: {} }; }

is essentially the same as:

var groupManager = groupManager || {groups: {}};

But the GroupManager so instantiated lives only within the if()
block.
That is an incorrect conclusion, there is no block scope in
javascript.

*And to include an initial var GroupManager simply kills state.
Only if you unconditionally assign a value, the declaration by itself
never changes the value per ECMA-262.

Are there any techniques for allowing multiple includes of a
Javascript file while detecting a previous include, allowing me to
preserve any existing variables?
I don't think detecting script file includes is a suitable strategy,
you want to detect whether the variable already has a value and if so,
leave it alone and if not, initialise it.

A declaration with conditional assignment does just that.
--
Rob
Aug 27 '08 #3
rocketeer wrote:
I've a set of Javascript classes that maintain state. For example,
gm.js might be:

var GroupManager { groups: {} };

Over time I add new groups to the list:

GroupManager.groups[key] = myGroup;

If I include another Javascript file that also includes a reference to
include gm.js, or do this through an Ajax call, this new loading of
gm.js overwrites my existing GroupManager variable and I lose all my
accumulated data.

So I thought I could do

if(!GroupManager) { GroupManager = { groups: {} }; }
That should almost work. This:

if(!GroupManager) {
var GroupManager = { groups: {} };
}

will do the trick. What RobG posted will work, too. So will:-

var GroupManager;
if(!GroupManager) {
GroupManager = { };
}
- or go directly to the global object with:-

if(!this.GroupManager) {
this.GroupManager = { groups:{} };
}

All work.
But the GroupManager so instantiated lives only within the if()
block. And to include an initial var GroupManager simply kills state.
By now you know that is not true. The relevant spec is Ecma-262 r3,
section 10.1.3.

The variable declarations are taken first, before statements so the
effect would be:

var GroupManager; // in first pass.

if(!GroupManager){ // in second pass.
GroupManager = { groups: {} };
}
Are there any techniques for allowing multiple includes of a
Javascript file while detecting a previous include, allowing me to
preserve any existing variables?
You could safely use the first one I tweaked. Why? Well, because what
happens when a variable statement is encountered is that if there is
already property of the variable object with that same name, then it's
value is not changed, however if there is not already a variable of that
same name, it's value is undefined.

So, if GroupManager was already defined, then the variable declaration -
var GroupManager; - wouldn't change its value.

But if GroupManager was not already defined, then the variable
GroupManager would have value undefined, and the statement
if(!GroupManager) would be equivalent to if(!undefined), which would
result true.

You're good to go, and you don't need eval.

Garrett
Thanks,
Jerome
Aug 28 '08 #4
On Aug 27, 8:45*pm, dhtml <dhtmlkitc...@gmail.comwrote:
rocketeer wrote:
I've a set of Javascript classes that maintain state. *For example,
gm.js might be:
[snip]
var GroupManager;
if(!GroupManager) {
* *GroupManager = { };

}
[snip]
So, if GroupManager was already defined, then the variable declaration -
var GroupManager; - wouldn't change its value.

But if GroupManager was not already defined, then the variable
GroupManager would have value undefined, and the statement
if(!GroupManager) would be equivalent to if(!undefined), which would
result true.

You're good to go, and you don't need eval.

Garrett
[snip]

Thanks for the advice (Mr. PointedEars (above) is a bummer...). I
don't have any problems with the MSIE browser. But I have to support
FireFox, too. When I do this:

var GroupManager = GroupManager || { groups: {} };

or the if(...) version, FireFox 3 (I no longer have version 2 around
anywhere) goes off to odd places (wyciwyg://1/http://and_so_on). Why
and where it goes isn't as important as I don't get the desired
behavior. So unless there is another tweak here I'll have to live
with my initial situation.

I think I will look at the EMCAScript specification. I figured that
var worked like (name your favorite language) and cleared an existing
variable having that name. So I'll slowly learn ...

Jerome.
Aug 28 '08 #5

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

Similar topics

11
by: Steven T. Hatton | last post by:
I've made no secret of the fact that I really dislike the C preprocessor in C++. No aspect of the language has caused me more trouble. No aspect of the language has cause more code I've read to be...
28
by: Benjamin Niemann | last post by:
Hello, I've been just investigating IE conditional comments - hiding things from non-IE/Win browsers is easy, but I wanted to know, if it's possible to hide code from IE/Win browsers. I found...
13
by: Andrew | last post by:
I use conditional compiler constants, set through the VBA IDE in Tools, <projectname> Properties, that I refer to throughout my code to control which code is used during development, and which...
12
by: wanghz | last post by:
Hi, Could I ask some questions about the conditional compilaion? Suppose I have three simple files: a.c, b.c and c.h /* --------a.c--------- */ #include <stdio.h> #include "c.h" int...
7
by: John Dolinka | last post by:
I have a project of several files with #defines to setup a conditional compile. I would like to put these #defines in a single file and change the conditional compile from this one file with the...
3
by: John | last post by:
Hi I have an app to deal with two companies A & B within a group of companies depending on which database is selected. Is it possible for setup to ask the user at install time which company they...
10
by: Dave | last post by:
I'm a C++ programmer of many years, trying to get my feet wet in C#. I have a question about conditional compilation. In C++, I would sometimes define a constant in an include file, and then...
43
by: dev_cool | last post by:
Hello friends, I'm a beginner in C programming. One of my friends asked me to write a program in C.The purpose of the program is print 1 to n without any conditional statement, loop or jump. ...
16
by: Alan Jones | last post by:
Hello everyone, any help would be greatly appreciated. :) What I'm trying to do may not be advisable, but here goes... I want a page named signature.php to appear conditionally as an include...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

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.