Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 10 of 10
Like Tree6Likes
  • 1 Post By sasha_bolcina
  • 1 Post By Ronald Roe
  • 2 Post By Ronald Roe
  • 2 Post By TheGAME1264

Thread: Variable Question

  1. #1
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times

    Variable Question

    So, I have a computer science-y question. Let's say I have a function, and I want to declare a boolean variable inside that function. I need the value to persist, so I declare it as a global. The function conditionally changes the value of that variable. No problem so far.

    Here's where I'm out of my depth. Because I declare that variable inside the function, it is reset to its initial value on subsequent calls to the function (for reference, the function is called once on DOM ready, then using an event listener for resize).

    So, my question is: how can I declare the variable inside the function, but not reset its value each time the function is called? I know I can just declare it outside the function, but I'd like to be able to keep everything wrapped up inside the function.

    Code:
    function functionName(){
      booleanVariable = false; // declares the variable as a global, but will set it back on subsequent calls
    
      if(someCondition){
        booleanVariable = true;
      } else {
        booleanVariable = false;
      }
    }
    Last edited by Ronald Roe; Nov 26th, 2014 at 04:09 PM.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  2.  

  3. #2
    Senior Member sasha_bolcina's Avatar
    Join Date
    Sep 2014
    Location
    Serbia
    Posts
    274
    Member #
    40099
    Liked
    47 times
    Variable has a block scope. If its declared in function, it will be destroyed when function is executed. Also, it is accessible only in function.
    Your variable
    booleanVariable = false;
    is not global as it is declared inside of function block, even it is not explicitly declared with 'var'. Javascript is creating var automatically.
    In OOP languages is something called "static variable" which is unique for all instances of a class.

    But, there is a way to make a variable in javascript similar to 'global' and 'static'. It has to be inside of a class, like this:

    Code:
    function ClassName(){
      // local, private variable
       var privateVarName = "some value";
      // public variable
       this.publicVarName = "other value";
       
       // get value of private variable (not accessible from outside of the class
       this.getPrivateVar = function(){
           return privateVarName;
       }
    }
    
    // create class prototype. It is loaded only once in memory, but accessible from all class objects
    // set new value to 'global' (public) var
    ClassName.prototype.setPublicVar = function (newValue) {
       this.publicVarName = newValue;
    }
    // same value is visible to all objects
    ClassName.prototype.getPublicVar = function () {    
       return this.publicVarName;
    };
    
    // creating object
    var myObject = new ClassName();
    // alert private var value
    alert(myObject.getPrivateVar());
    // set 'global' (visible to all objects, but not outside of class scope)
    myObject.setPublicVar("New text");
    // alert public var value
    alert(myObject.getPublicVar());
    Notice that variable is still not really global. It has its scope inside of class block, but objects instanced from the class can share its value.

    This I wrote is just example, and maybe will not work if executed...
    Last edited by sasha_bolcina; Nov 27th, 2014 at 03:00 AM.
    Ronald Roe likes this.

  4. #3
    Senior Member sasha_bolcina's Avatar
    Join Date
    Sep 2014
    Location
    Serbia
    Posts
    274
    Member #
    40099
    Liked
    47 times
    There is a nice explanation, better then I can do
    Private Members in JavaScript

  5. #4
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    I would just declare the variable outside of the function. If it requires a global scope, declare it globally. Why does it have to be declared inside of the function?
    Code:
    function functionName(){
      booleanVariable = false; // declares the variable as a global, but will set it back on subsequent calls
    
    
      if(someCondition){
        booleanVariable = true;
      } else {
        booleanVariable = false;
      }
    }
    var booleanVariable;
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  6. #5
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    Quote Originally Posted by sasha_bolcina View Post
    Your variable
    booleanVariable = false;
    is not global as it is declared inside of function block, even it is not explicitly declared with 'var'. Javascript is creating var automatically.
    Unless I'm misunderstanding it, this seems to disagree: Variable Scope (JavaScript)

    Also, once I've run the function, I can access the value of that variable from other functions or by simply calling it within the console.
    sasha_bolcina likes this.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  7. #6
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    Quote Originally Posted by TheGAME1264 View Post
    I would just declare the variable outside of the function. If it requires a global scope, declare it globally. Why does it have to be declared inside of the function?
    It doesn't have to, it's just a preference. I'd like to have it all packaged within the function for easy reuse by other people in the company I'm working for. I figured it out by the way: CodePen - A Pen by Ronald Roe

    In short, you test:
    Code:
    if(typeof booleanValue === 'undefined'){
      booleanValue = false;
    }
    // etc.,
    sasha_bolcina and TheGAME1264 like this.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  8. #7
    Senior Member sasha_bolcina's Avatar
    Join Date
    Sep 2014
    Location
    Serbia
    Posts
    274
    Member #
    40099
    Liked
    47 times
    @Ronald Roe

    I am sorry I completely miss the point and send here wrong information.
    You are totally right, and to be worst I knew that, so I have no idea why I wrote this... And I wrote something about "block scope", but I didn't mean it as programing term than just as description of the scope.

    Variable without keyword is global and set in window scope. Every variable is initially set as "undefined" and your solution is the right one.

    On the other side I'm not using global variables than classes and I wrote answer automatically without thinking in deep to find simple solution.

    Again, sorry for this :/

  9. #8
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    It's all good. What you wrote actally helped me word my search for the answer.

  10. #9
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    Quote Originally Posted by Ronald Roe View Post
    It doesn't have to, it's just a preference.
    Well you prefer WRONG! You must always do things the way I tell you to do them! You don't know ANYFING!
    Andrew Yurlov and Ronald Roe like this.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  11. #10
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    If anyone's curious, this is what I was working on: https://gist.github.com/ronaldroe/72d2bf86a262f82bd711

    If you've ever used meanMenu.js, you probably know it won't support multiple menus. This is a similar script that will.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 09:09 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com