-1

just look at this question: What is the scope of variables in JavaScript?

in the answer which is accepted look at the point 3

according to him a will be 4 but now look at my function:

function JIO_compiler(bpo){  // bpo means that " behave property object " and from here i will now start saying behave property to behave object

var bobj = bpo, // bobj = behave object
    bobj_keys =  Object.keys(bobj), // bobj_keys = behave object keys. This willl return an array 
    Jcc_keys = Object.keys(JIO_compiler_components), // Jcc = JIO compiler components
    function_code = ""; // get every thing written on every index

    if (bobj.hasOwnProperty('target') === false) { // see if there is no target defined
        console.log("No target is found"); // tell if there is no target property
        throw("there is no target set on which JIO-ASC will act"); // throw an error
    };
    if (bobj.hasOwnProperty('target') === true) {
        console.log("target has been set on "+ bobj['target']+" element"); //tell if the JIO-ASC has got target
        function x(){
            var target_ = document.getElementById(bobj['target']);
            if (target_ === null) {throw('defined target should be ID of some element');};
            function_code = "var target="+target_+";"; 
            console.log("target has successfully been translated to javascript");//tell if done
        };
    };

    for(var i = 0; i < bobj_keys.length; i++){

        if(bobj_keys[i] === "$alert"){ // find if there is  $alert on any index
            var strToDisplay = bobj[bobj_keys[i]]; 
            var _alert = JIO_compiler_components.$alert(strToDisplay);
            function_code = function_code+ _alert;
        };



    };// end of main for loop
alert(function_code);
 new Function(function_code)();
};

well it is big... but my problem is in the second if statement. now according to the accepted answer the value of function_code should change according to what is instructed. but when at last i alert the function code then it alert blank. i mean it should alert at least var target = something ; and the last console.log statement of this if statement is not showing text in the console.

so what is wrong in this ?

Community
  • 1
  • 1
anni
  • 290
  • 1
  • 3
  • 15
  • sorry for my grammar... – anni Nov 06 '14 at 03:45
  • 1
    BTW, this code will not work in strict mode. Putting a function declaration (e.g. `function x(){...})` inside an if block is seen as a function statement, which isn't allowed. Move the function declaration outside the block. Oh, and *throw* is not a function, so `throw "there ...";`. – RobG Nov 06 '14 at 04:05

2 Answers2

1

You're setting function_code inside the definition for function x(), but x() is never called. function_call won't change until you make a call to x.

sherb
  • 5,845
  • 5
  • 35
  • 45
1

thats because your variable is inside the function scope, you need to define your variable outside of it, as

function_code = "";
function JIO_compiler(bpo){ 
    ....

};// end of main for loop
//call the function
JIO_compiler(some_parameter);
//alert the variable
alert(function_code);

you need to call the function JIO_compiler() first so that the appropriate value is set to function_code variable from JIO_compiler() function, as

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162