Forum Moderators: open

Message Too Old, No Replies

still on the basics

simplifying functions

         

ctoz

11:00 am on Aug 14, 2007 (gmt 0)

10+ Year Member



With a series of functions that share the same variables, but do different things with the output, there must be a way of not having to repeat the variables for each of the functions? As in (with previous help from nshiell)

function DoITonce() {
var a = One() // a math.random function
var b = Two() // a math.random function
var c = Three() // a math.random function
var x = (a + b + c) // =6, 7, 8 or 9

switch(x) {
case 6:some functions();
break
case 7:some more functions();
break
case 8: somefunctions();
break
default: somemore functions();
}

and then
function doITagain() {
var a = One()
var b = Two()
var c = Three()
var y = (a + b + c)

switch(y) {
case 6:some xfunctions();
break
case 7:some more fxunctions();
break
case 8: somexfunctions();
break
default: somemore xfunctions();
}

and then
doITmore() ... using var z = (a + b + c) and switch(z) ...

Any pointers appreciated.

Fotiman

3:05 pm on Aug 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There are a couple of options. The old method would have been to create a global variable. For example:


// These are global, not scoped in a particular function
var a = One() // a math.random function
var b = Two() // a math.random function
var c = Three() // a math.random function
function DoITonce() {
var x = (a + b + c) // =6, 7, 8 or 9
...
}
function doITagain() {
var y = (a + b + c)
...
}

But global variables are sloppy and more recent methods instead create a "namespace" to contain all of your variables and methods. That way, instead of having a bunch of global variables and functions (which could potentially conflict with someone else's code if you decide to add a 3rd party script to your page), you only have a single global object, thus reducing the chances for conflicts and keeping your code modularized and easily transportable.

Try Googling for Javascript namespace for more on this method.

ctoz

11:01 am on Aug 15, 2007 (gmt 0)

10+ Year Member



Thanks: I'll probably go sloppy to start, being really slow and there' a way to go, but I've had a namespace google and understand a bit more.

Just on the point of conflicting scripts, I posted earlier without a reply, but maybe it's worth another try: is there any obvious reason why these scripts should conflict?—

//change by ID ...Code_Punk

function box (boxname,menustate) {
document.getElementById(boxname).style.visibility = menustate}

// called by box('boxname', 'menustate')
//and change by class ...Shawn Olson

function changecss(theClass,element,value) {

var cssRules;
if (document.all) {
cssRules = 'rules';
}
else if (document.getElementById) {
cssRules = 'cssRules';
}
for (var S = 0; S < document.styleSheets.length; S++) {
for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
document.styleSheets[S][cssRules][R].style[element] = value;
} } } }

//called by, eg changecss('.myClass','color','black')

...both being used within a single switch case.

CT