为什么当“var”在函数范围内时,它的值不发生变化
why value is not changing of a `var` when it is in the scope of the function
看看这个问题:JavaScript中变量的作用域是什么?
在被接受的答案中,查看点3
根据他的说法,a
将是4,但现在看看我的函数:
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)();
};
它很大。。。但我的问题在第二个if语句中。现在根据所接受的答案,CCD_ 2的值应当根据所指示的内容而改变。但当我最后提醒函数代码时,它会提醒空白。我的意思是,如果语句没有在控制台中显示文本,它应该至少提醒var target = something ;
和最后一个console.log
语句注意这一点。
那么这里面出了什么问题呢?
您在function x()
的定义中设置function_code
,但从未调用x()
。在您呼叫x
之前,function_call
不会更改。
这是因为您的变量在函数范围内,您需要将它之外的变量定义为
function_code = "";
function JIO_compiler(bpo){
....
};// end of main for loop
//call the function
JIO_compiler(some_parameter);
//alert the variable
alert(function_code);
您需要首先调用函数JIO_compiler()
,以便从JIO_compiler()
函数中将适当的值设置为function_code
变量,如
相关文章:
- 控制台返回var不是't定义,但它是
- 从var向代码隐藏函数传递值
- 如何通过引用var Using DataTables来进行分页或排序
- 单击页面上的链接后高度发生变化
- jQuery语法添加了var
- 如何将具有文本类型值的var放入jQuery函数中
- JS:检查URL中的参数,然后迭代一个参数为var的函数
- 当var==0时,我如何显示一个警报
- React redux初始化功能,无论状态变化如何
- 角度ng变化或ng点击选择can'不起作用
- javascript中对象构造函数中的var属性与this.properties
- 每个选择器的Jquery css颜色都在变化,但字体大小却没有变化
- 函数中this和var之间的区别
- JavaScript-的含义:var DndUpload=函数(inputElem){};
- JS幻灯片与CSS背景颜色变化
- Understanding Javascript scope with "var that = this&qu
- 如何使用javascript var引用Mongo DB集合's字段
- 重新加载页面时Javascript变量发生变化
- 如何获得Bootstrap Datepicker的年和月,因为它在变化
- 为什么当“var”在函数范围内时,它的值不发生变化