SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    May 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jquery variable scope

    First of all to all sp forum users Hello , im new tot this forum

    I have a question about variable scope of JQuery. I have used the search function first but it still doenst really make sence to me how variable scope works, and when to use var in front of a variable.

    can someone tell me how :
    when or why to use var in front of variable?
    how i can acces a variable in a function ?

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,196
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    when or why to use var in front of variable?
    You should always use var before any new variable been declared in the script as it helps to avoid getting errors throughout the script. The only time you don't need var is when your pushing information into an array or updating that variable with a new value.

    For instance, using the following example below will work fine but will consume a very tiny bit more browser memory to process it.
    Code JavaScript:
    var hi = 'Hello';
    var hi = 'Bye';

    A better way to do is this is by using the example below
    Code JavaScript:
    var hi = 'Hello';
    hi = 'Bye';

    NOTE: You also need to use the above example when working with arrays.

    how i can acces a variable in a function ?
    Its actually very simple but can also be hard depending how you setup your global and local scope variables. Below is an example of a var thats apart of the global scope
    Code JavaScript:
    var hi = 'hello';
     
    function saySomething() {
        alert(hi);
    }
     
    saySomething();

    This will work fine but for instance if you set a variable within another function you can only access it within that local scope of the function, you can get around this by either setting another var in the global scope of passing the var directly to the function you need to use it in. See the examples below...

    Pass to a global scope var
    Code JavaScript:
    var hi;
     
    function message() {
        hi = 'hello';
    }
     
    message();
     
    function saySomething() {
        alert(hi);
    }
     
    saySomething();

    Pass directly to the function
    Code JavaScript:
    function message() {
        var hi = 'hello';
        saySomething(hi);
    }
     
    function saySomething() {
        alert(hi);
    }
     
    message();

    Hope that helps.

  3. #3
    SitePoint Member
    Join Date
    May 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah dit maakt het inderdaad duidelijker bedankt het laatste voorbeeld werkt niet

    Quote Originally Posted by Google Translate
    Ah this makes it really clear thanks to the last example does not work
    Code JavaScript:
    function message() {
        var hi = 'hello';
        saySomething(hi);
    }
     
    function saySomething(para) { // <--moet hier niet een parameter staan ?
        alert(para);
    }
     
    message();
    Last edited by AnthonySterling; Jun 1, 2011 at 01:19. Reason: Added translation

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,196
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Ah this makes it really clear thanks to the last example does not work
    Yes it should be, i have a habit of not fully proof checking my own code


Tags for this Thread

Bookmarks

Posting Permissions

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