-2

How to declare a variable that its value can be changed globally by functions?

var a = 0;
var b = 0;

function c() {a = 5};
function d() {b = 6};

function e() {console.log(a+b)};

c();
d();
e();  // I expect here: 11
Rich
  • 705
  • 9
  • 16
  • 11
    `e()` does log `11`... – Esailija Jun 10 '12 at 13:13
  • 1
    on console is 11, what's the problem? – Sam Jun 10 '12 at 13:15
  • a and b are global variables in this case, result will be 11 – Dhiraj Jun 10 '12 at 13:15
  • You're already doing it...I think it should be working – Saad Imran. Jun 10 '12 at 13:15
  • 1
    As far as I understand, he is trying to create `static` variables as his question title says which are possible as **function properties** (as he says `globally by functions`), I answered that question but probably it might not be what he is looking for since people here were fast enough to down vote within few seconds. – Sarfraz Jun 10 '12 at 13:19
  • @Sarfraz You should consider reposting your answer, but with a bit of an explanation and not just some code. – Niko Jun 10 '12 at 13:23
  • @Niko: I was editing the answer, in the mean time, i got 5 down votes which made me rather delete it than edit it further, fast sunday today :) – Sarfraz Jun 10 '12 at 13:24
  • Thank you Esailija. I use google AppScript and it doesn't work in that way. – Rich Jun 10 '12 at 13:26
  • @user1405507 You need to improve the question, e.g. add more details and clarify what the situation and the desired outcome is. Otherwise, this won't get reopened. – Niko Jun 10 '12 at 13:28
  • Thank you Niko. Unfortunately that's the most precise one that I can issue. – Rich Jun 10 '12 at 13:33
  • I think he meant the kind of `static` variables you use e.g. in C on the file level: Global (but only accessible within that .c file) – ThiefMaster Jun 11 '12 at 10:04

1 Answers1

2

Is that what you want? You can find lots of example when you search it.

  function count() {
        alert(count.num);
        count.num++;
    }

    // initialize count number
    count.num = 0;

    foo(); // alert 0
    foo(); // alert 1
Omer SOYER
  • 41
  • 5
  • Thank you Sarfraz, but not. Here we have a function which acts automatically when it is called. I need a GLOBAL STATIC variable which value's can be modified by function in a way that this change must be a global one. I mean every other function should "see" the modified value. – Rich Jun 10 '12 at 13:38
  • Sorry for everyone. This was my first post here and I made a mistake as per didn't mentioned or labeled that I tent to use Google AppScript. Regarding this fact the answer is here: https://developers.google.com/apps-script/class_cache – Rich Jun 17 '12 at 19:15