List Info

Thread: JavaScript Constants




JavaScript Constants
user name
2006-12-28 02:02:36
I do most JavaScript programming with objects and methods. I
use tiny
boot and shutdown scripts as hooks for the browser load and
unload
events.

My JavaScripts interact with scripts running on the web
server. I need
to declare shared values (as constants) to assist
communication between
client and server. I declare constants using global
variables:

var CONSTANT_NAME = 'whatever';

I would prefer to avoid the global scope. I am also aware
that the
"constant" value can be modified by running code.

I toyed with the idea of a special Constant object that
would dispense
a shared value in response to invocation of a hardcoded
method. I
haven't attempted this approach yet, so it may have its own
special
complications.

Do you know of an alternative to global variables for shared
constants
that is cross browser?

Thanks,

Gerard Vignes
http://www.GerardVignes.c
om
Seattle, WA


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the
Google Groups "OOP Javascript" group.
To post to this group, send email to OOP-Javascriptgooglegroups.com
To unsubscribe from this group, send email to
OOP-Javascript-unsubscribegooglegroups.com
For more options, visit this group at ht
tp://groups-beta.google.com/group/OOP-Javascript
-~----------~----~----~----~------~----~------~--~---

Re: JavaScript Constants
user name
2007-02-02 19:55:18
If you don't want it to be global, then it must be hidden.

What about this?  It limits the global impact to two
variables (which
could be linked together as members of a single namespace
object, of
course.)

var const, isset;
(function(){
var CONSTANTS={};
var KEYS={};
isset=function(key) {
 return KEYS[key];
};
const=function(key,val) {
 if( !isset(key) ) {
  KEYS[key]=true;
  CONSTANTS[key]=val;
 }
 return CONSTANTS[key];
};
})();

alert(isset('myConstant')); // "false"
const('myConstant',123);
alert( const('myConstant') ); //alerts 123
const('myConstant','234');
alert( const('myConstant') ); // still alerts 123
alert(isset('myConstant')); // "true"


Of course, a better approach would probably be to embrace
the lambda
nature of the Javascript language and rethink your need for
global
"constants."


Another way of writing the same thing, if you prefer the
speed and
lightness of with-blocks:

var const, isset;
with ({
 CONSTANTS:{},
 KEYS:{}
}) {
isset=function(key) {
 return KEYS[key];
};
const=function(key,val) {
 if( !isset(key) ) {
  KEYS[key]=true;
  CONSTANTS[key]=val;
 }
 return CONSTANTS[key];
};
}


--
Isaac Z. Schlueter
http://isaacschlueter.com


On Dec 27 2006, 6:02 pm, "www.gerardvignes.com"
<gerardvig...gmail.com> wrote:
> I do most JavaScript programming with objects and
methods. I use tiny
> boot and shutdown scripts as hooks for the browser load
and unload
> events.
>
> My JavaScripts interact with scripts running on the web
server. I need
> to declare shared values (as constants) to assist
communication between
> client and server. I declare constants using global
variables:
>
> var CONSTANT_NAME = 'whatever';
>
> I would prefer to avoid the global scope. I am also
aware that the
> "constant" value can be modified by running
code.
>
> I toyed with the idea of a special Constant object that
would dispense
> a shared value in response to invocation of a hardcoded
method. I
> haven't attempted this approach yet, so it may have its
own special
> complications.
>
> Do you know of an alternative to global variables for
shared constants
> that is cross browser?
>
> Thanks,
>
> Gerard Vigneshttp://www.GerardVignes.c
om
> Seattle, WA


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "OOP Javascript" group.
To post to this group, send email to OOP-Javascriptgooglegroups.com
To unsubscribe from this group, send email to
OOP-Javascript-unsubscribegooglegroups.com
For more options, visit this group at ht
tp://groups-beta.google.com/group/OOP-Javascript
-~----------~----~----~----~------~----~------~--~---


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )