1

http://jsfiddle.net/ujapned5/

I'm trying to make an "initializer" function that will be used to define CSS id and class names.

function init() {

    //our empty array for pushing
    var DivNamingPushArray = [];

    //object where we define our properties that will be used later
    var namingScheme = {
        "parentId": {
            "firstChild": "blah",
            "secondChild": "blahblah",
            "thirdChild": "blahblahblah"
        }
    }

    //loop through the namingScheme object and push into array
    for (propOne in namingScheme) {
        DivNamingPushArray.push(propOne);
        for (propTwo in namingScheme[propOne]) {
            DivNamingPushArray.push(namingScheme[propOne][propTwo])
        }
    }
}

function execute() {

    //call the "init" function
    init();

    //this cannot be called
    console.log(DivNamingPushArray);

    //however, why can this be successfully called?
    console.log(propOne);
}

execute();

I would really prefer to keep these functions separate, instead of including execute inside init. There are other functions that will need to call the DivNamingPushArray variable later on.

I am looking through the MDN documentation on variable scope but not finding a simple solution...

1
  • 3
    Please note that if you don't declare propOne and propTwo they will be global. Using strict mode would prevent these kind of potential future problems Commented Jan 30, 2015 at 14:56

5 Answers 5

1

I'd suggest you use a third function to separate your logic

As for the propOne, you can see my comment in your question, just put var in front of it.

Working example here

execute();

function init(app) {
    app.DivNamingPushArray = getNamingArray();
}

function execute() {
    var app = {};

    //call the "init" function
    init(app);

    console.log(app.DivNamingPushArray);

}

function getNamingArray() {
    //our empty array for pushing
    var DivNamingPushArray = [];

    //object where we define our properties that will be used later
    var namingScheme = {
        "parentId": {
            "firstChild": "blah",
            "secondChild": "blahblah",
            "thirdChild": "blahblahblah"
        }
    }

    //loop through the namingScheme object and push into array
    for (var propOne in namingScheme) {
        DivNamingPushArray.push(propOne);
        for (var  propTwo in namingScheme[propOne]) {
            DivNamingPushArray.push(namingScheme[propOne][propTwo])
        }
    }

    return DivNamingPushArray;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This will definitely come in handy as I continue to learn best OOP practices.
0

I suggest to use some js pattern like module to separate it:

http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript

and the using something like observer to let function comunicate.

This is not a simple implementing solution, but you can have fun with it!

Comments

0

If you want DivNamingPushArray accessible within the scope of execute(), take its declaration one level upper, before init() declaration :

//Declare your array outside of your init function
var DivNamingPushArray = [];

function init() {
   //...

For propOne, see Amit Joki's answer.

Comments

0

When you declare a variable without the keyword var, it becomes global. Add var before you declare the variables in for...in loop.

for (var propOne in namingScheme) {
    DivNamingPushArray.push(propOne);
    for (var propTwo in namingScheme[propOne]) {
        DivNamingPushArray.push(namingScheme[propOne][propTwo])
    }
}

When a variable becomes global, or part of window, it can be accessed anywhere. So, always use var to keep the variables in scope and thereby avoiding the pollution of global scope.

After you've called init, propOne and propTwo have become global variables and therefore can be accessed anywhere. As to DivNamingPushArray, declare it one-level higher, so it will be visible to other functions or try to use module pattern.

1 Comment

@aduch it did already, but yet, I've made it even more clear.
0

Javascript variable scoping works like this:

//this will not be changed in "init" function.
DivNamingPushArray = [];

//this will be changed because you don't localize your variable in init function
propone = 'value'; 

function init() {

    //this "var" declaration localizes the variable. 
    //You can do anything you want with it and 
    //the global DivNamingPushArray wont change.
    //The only way to use the global variable now is through 
    //the global object. (window.DivNamingPushArray for browsers)
    var DivNamingPushArray = [];

    // if you would have used "for (var propOne in namingScheme)",
    // the variable would have been local
    for (propOne in namingScheme) {
        // ...
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.