global variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mr_burns

    global variables

    hi,

    i was wondering if anybody could tell me when a function is called and
    variables are defined within that function, are the variables global
    where i can call and modify the variables in other functions.

    for example:



    function fdosomethingfir st() {

    //this function will be called first

    var v_num1, v_num2

    v_num = 1234;

    }

    function fdosomethingaga in() {

    //if i were to do something in this function, could i use the
    variables from the previous function???

    }


    if the function - fdosomethingfir st() - was called first, is it
    possible to use the variables used in that in - fdosomethingaga in()??
    i would try this out but im at work now and really need to know later
    on. much appreciated.

    burnsy
  • Dennis Biletsky

    #2
    Re: global variables

    No, if you declare variable in function it will be local. If you need global
    declare variable not in function like this

    var v_num1, v_num2

    function fdosomethingfir st() {


    v_num = 1234;

    }

    function fdosomethingaga in() {


    }

    "mr_burns" <bissatch@yahoo .co.uk> ???????/???????? ? ???????? ?????????:
    news:651c6ea9.0 404020230.54e99 62d@posting.goo gle.com...[color=blue]
    > hi,
    >
    > i was wondering if anybody could tell me when a function is called and
    > variables are defined within that function, are the variables global
    > where i can call and modify the variables in other functions.
    >
    > for example:
    >
    >
    >
    > function fdosomethingfir st() {
    >
    > //this function will be called first
    >
    > var v_num1, v_num2
    >
    > v_num = 1234;
    >
    > }
    >
    > function fdosomethingaga in() {
    >
    > //if i were to do something in this function, could i use the
    > variables from the previous function???
    >
    > }
    >
    >
    > if the function - fdosomethingfir st() - was called first, is it
    > possible to use the variables used in that in - fdosomethingaga in()??
    > i would try this out but im at work now and really need to know later
    > on. much appreciated.
    >
    > burnsy[/color]


    Comment

    • mscir

      #3
      Re: global variables

      mr_burns wrote:
      [color=blue]
      > i was wondering if anybody could tell me when a function is called and
      > variables are defined within that function, are the variables global
      > where i can call and modify the variables in other functions.
      > for example:
      >
      > function fdosomethingfir st() {
      > //this function will be called first
      > var v_num1, v_num2
      > v_num = 1234;
      > }
      >
      > function fdosomethingaga in() {
      > //if i were to do something in this function, could i use the
      > variables from the previous function???
      > }
      >
      > if the function - fdosomethingfir st() - was called first, is it
      > possible to use the variables used in that in - fdosomethingaga in()??
      > i would try this out but im at work now and really need to know later
      > on. much appreciated.
      >
      > burnsy[/color]

      You might want to read up on variable scope, google has lots of hits,
      here are a few pages that explain global and local variable scope:






      Good Luck,
      Mike

      Comment

      • Michael Winter

        #4
        Re: global variables

        On 2 Apr 2004 02:30:34 -0800, mr_burns <bissatch@yahoo .co.uk> wrote:

        [snip]
        [color=blue]
        > function fdosomethingfir st() {
        >
        > var v_num1, v_num2[/color]

        These are declared local in the function. Once the function exits, they
        won't exist.
        [color=blue]
        > v_num = 1234;[/color]

        This is a previously undeclared variable. Because it doesn't use the var
        keyword, it is declared global.
        [color=blue]
        > }
        >
        > function fdosomethingaga in() {
        >
        > //if i were to do something in this function, could i use the
        > //variables from the previous function???[/color]

        You could use v_num as it's a global variable, but not v_num1 and v_num2.
        [color=blue]
        > }[/color]

        There are a few ways around this.

        1. Declare all shared variables global.

        This is a simple approach, but it's the least attractive from both a
        theoretical and practical point of view (no link between data and
        behaviour, and data accessable by all which could lead to name clashes).


        2. Pass the variables as parameters to the second function.

        This should need no explanation.


        3. Use an object.

        You could create an object that has the variables v_num1 and v_num2 as
        properties of the object, and the two functions as methods of the same
        object.

        There are a couple of ways to apply this.

        - Create an object (either by constructor or literal):

        var v = {
        this.fdosomethi ngfirst = function() {
        this.num1 = ??; // On first write to these variables, they
        this.num2 = ??; // will be created as properties of the object
        }
        this.fdosomethi ngagain = function() {
        // ...
        }
        }

        Alternatively, you could make the variables private, where only the
        methods of that object can access them:

        var v = {
        var num1, num2;

        this.fdosomethi ngfirst = function() {
        num1 = ??; // Don't use the 'this' operator this time
        num2 = ??;
        }
        this.fdosomethi ngagain = function() {
        // ...
        }
        }

        This is an application of closures.


        - Return an object from the first function which is passed to the second

        function fdosomethingfir st() {
        // ...
        return { num1: <value>, num2: <value> };
        }
        function fdosomethingaga in( obj ) {
        var v_num1 = obj.num1;
        var v_num2 = obj.num2;
        }

        var retObj = fdosomethingfir st();
        fdosomethingaga in( retObj );


        Does that help in any way?

        Mike

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        Working...