Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Apr 2003
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Where to declare vars?

    Hello

    var one = 0;

    function do_something()
    {
    var two = 1;
    }

    <onClick="do_something()"> // attached to a button

    I know the variable 'one' gets created once as the webpage scripts are first 'read' by the browser.

    Does a variable called 'two' get created every time the function do_something() is called?

    some scripts i have seen have variables being created within functions and i thought they should be created outside of functions - maybe i have misunderstood something here

    thanks

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    I think... var two is a local variable. It only exists within the scope of the function it's declared in. Var one is a global one - it exists within the scope of the page, and can be accessed by any function. As a rule of thumb, declare vars globally (ie: outside functions) to reduce likelihood of "is null or is not an object" woes.

  • #3
    New Coder
    Join Date
    Apr 2003
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the reply

    does that mean that every time the do_something() function is called, a new variable called two is created, exists for the 'lifetime' of the function, then disappears after the function call is over.

    in other words, if the button is clicked three times, are there three variables all called two - each having been created, used and then... who knows or just one that keeps being created, used and then destroyed?

    could you clarify this for me?

    thank you

  • #4
    New to the CF scene
    Join Date
    Mar 2003
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool

    When a variable is declared inside of a function, it only lives as long as the function lives. So, let's say you call a function that has a variable defined in it. When the function is called, the variable is constructed and initialized. It is then accessed throughout the duration of the function call. When the function has completed, the variable is de-initialized and de-constructed and the memory space it was using is released.

    So, in summary, if you have a variable that is inside of a function and call that function three times, the variable will be created and destroyed three times (Each time the function is called).

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Everything in client-side scripting is either an object or a property of an object (often both). So-called 'global' variables are actually properties of the window object, which, for all intents and purposes can be considered the global object in the client-side environment (the actual global object being inaccessible).

    Every time you call a function, a call object belonging to that function object is created, and any variables local to the function - including parameter variables (arguments) become properties of it. Contrary to the above suggestion, it is not good practice to globally declare all your variables; those that are only relevant to a particular function, and will not need to hold their values between function calls, should always be declared (with the var keyword) within the function body. Omitting the keyword makes them global. Why clutter up the global (window's) namespace with unnecessary data? Enough floating around there already (see: IE).

    Function-scope variable behavior is actually more complex in JS that it might seem; see crockford.com for more about nested functions.

  • #6
    New Coder
    Join Date
    Apr 2003
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you all

    i understand now


  • #7
    New to the CF scene
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    can it be declared as static

    in vb you can declare a local variable as static and it will hold its value between calls to that method can you do the same in jscript

    i have been studying java and understand that declaring a variable as static prevents an instance of a variable being created for instance of a class, does the same apply in jscript


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •