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...
propOneandpropTwothey will be global. Using strict mode would prevent these kind of potential future problems