passing parameters

G

Greg-WBSR

Would anyone be kind enough to tell me how to go about adding a
parameter to this function....

function toggleVis() {
isVisible = !isVisible;
networkLink.setVisibility(isVisible);
reportState();
}

I want to pass a parameter - eg 'green'
eg toggleVis('green');

so the end result equals this function

function greentoggleVis() {
greenisVisible = !greenisVisible;
greennetworkLink.setVisibility(greenisVisible);
reportGreenState();
}

I have a lot of files (green,blue,black etc) to toggle, and would like
to be able to use the one function and just pass the name of the file.
Right now I have 1 function per file which is pain to copy and edit
every time a new file gets created.

currently I am also creating global variables - greenisVisible and
greennetworkLink
is that needed???

I thought it would be something like

function toggleVis(file) {
(file)isVisible = !(file)isVisible;
(file)networkLink.setVisibility((file)isVisible);
report(file)State();
}

but obviously I am wrong.
any help would be appreciated

Thanks
Greg
 
G

Greg-WBSR

Would anyone be kind enough to tell me how to go about adding a
parameter to this function....
 function toggleVis() {
      isVisible = !isVisible;
      networkLink.setVisibility(isVisible);
      reportState();
    }
I want to pass a parameter - eg 'green'
eg toggleVis('green');
so the end result equals this function
function greentoggleVis() {
      greenisVisible = !greenisVisible;
      greennetworkLink.setVisibility(greenisVisible);
      reportGreenState();
    }

In theory, you could try adding a 'color' argument to the function, and
addressing the global variables like greenisVisible as

  this[color + "isVisible"]

but I don't recommend it (see below).
I have a lot of files (green,blue,black etc) to toggle, and would like
to be able to use the one function and just pass the name of the file.
Right now I have 1 function per file which is pain to copy and edit
every time a new file gets created.
currently I am also creating global variables - greenisVisible and
greennetworkLink
is that needed???

No, it isn't. This type of design is hard to maintain and can lead to
problems later on, even if it appears to work at the moment. It would be
better to arrange your data (files, networkLinks, etc) in a more
object-oriented way.
I thought it would be something like
 function toggleVis(file) {
      (file)isVisible = !(file)isVisible;
      (file)networkLink.setVisibility((file)isVisible);
      report(file)State();
    }

Personally, I would probably implement toggleVis() and reportState() as
methods of the respective file objects, and isVisible and networkLink as
object properties. I don't know how your data is set up, but from what
you told us, it might work like this:

  // minimal File constructor + prototype:

  function File (color, link) {
    this.color = color;
    this.networkLink = link;
    this.isVisible = false;  // default?
  }

  File.prototype = {
    toggleVis: function () {
      this.isVisible = !this.isVisible;
      this.networkLink.setVisibility(this.isVisible);
      this.reportState();
    },
    reportState: function () {
      // doSomething(this.isVisible);
    }
  };

  // example usage

  var greenFile = new File("green", someNetworkLink);
  greenFile.toggleVis();

  // example with several different files:

  var files = {
    green: new File("green", networkLink1),
    black: new File("black", networkLink2),
    blue:  new File("blue",  networkLink3)
  };

  files["black"].toggleVis();

  // or, with toggleVis as a stand-alone function:

  var files = { /* as above */ };

  function toggleVis (color) {
    var file = files[color];
    file.isVisible = !file.isVisible;
    file.networkLink.setVisibility(file.isVisible);
    file.reportState(file.isVisible);
  }

  toggleVis("black");

This is only an untested minimal example, of course. Depending on your
actual requirements, it will probably look a little different, but you
get the idea.

Sorry about the late reply (especially since you were so quick to
answer)

I am having troubles implementing your suggestions, but am still
working on it.

The part I want is

function toggleVis (color) {
var file = files[color];
file.isVisible = !file.isVisible;
file.networkLink.setVisibility(file.isVisible);
file.reportState(file.isVisible);
}

toggleVis("black");

what do I need to be able to us that (ie declaring variables)
basically I am not sure which of the rest of your functions (or all)
do I need???

Thanks for your help
Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top