Javascript 变量未更改值
因此,我正在设计一个代码,使用户能够创建伪自定义操作,可以在一个特殊的Javascript 变量未更改值,javascript,object,while-loop,var,Javascript,Object,While Loop,Var,因此,我正在设计一个代码,使用户能够创建伪自定义操作,可以在一个特殊的eval()函数下使用(因为JavaScript不是一种可扩展语言)。我的问题是,似乎只有创建的第一个变量被注册和计算 我在这里发布了一大段代码 var CMD = function(){ var objs = gAO() /* gets all of the objects */; // testing for other instances of the CMD object. this .bool
eval()
函数下使用(因为JavaScript不是一种可扩展语言)。我的问题是,似乎只有创建的第一个变量被注册和计算
我在这里发布了一大段代码
var CMD = function(){
var objs = gAO() /* gets all of the objects */;
// testing for other instances of the CMD object.
this .bool = 0;
for(obj in objs) this .bool ^= !objs[obj]["_aqz39"] // boolean
if(this .bool){
// DEFINING VARS
this .objs = objs;
this["_aqz39"] = true;
this .ops = []; this .eqs = [];
}
}
{ /* init */
var cmd = new CMD();
}
// USER INPUT FOR CREATING 'NEW VARIABLES'
var Operator = function(op,input){
// SYNTAX: "<operator>","x <operator> y = <result, using 'x' and 'y'>"
// EXAMPLE: "#","x # y = 3 * x - y"
this .op = op;
this .eq = input.split("=")[1].trim();
}
// FUNCTION FOR ACTIVATING THE VARIABLE TO BE
// ...RECOGNIZED BY THE CMD's 'EVAL' FUNCTION
activate = function(ind){
cmd.ops.push(ind.op);
cmd.eqs.push(ind.eq);
}
CMD.prototype.eval = function(equ){
// DECLARING VARS
var t = cmd,oper,equation,x,y,i=0;
// LOOPS THROUGH ALL OF THE CHILDREN OF cmd.ops
while (i < t["ops"].length){
// CHECKS TO SEE IF THE INPUT CONTAINS THE SYMBOL
if(equ.search(oper) !== -1){
// the operator
oper = t["ops"][i];
// the equation
equation = t["eqs"][i];
// from the first index to the beginning of the operator
x = equ.slice(0,equ.search(oper)).trim(),
// from right after the operator to the end of the thing
y = equ.slice(equ.search(oper)+1,equ.length).trim();
/* INFORMATION LOGGING */
console.log({x:x,y:y,oper:oper,equation:equation,i:i,t:t,bool: equ.search(oper),len:t["ops"].length})
// RESULT
return eval(eval(equation));
}
// INCREMENTS 'i'
i++;
}
// ELSE
return false;
}
测试#2
我已经排除了大约一个小时的故障,我不知道出了什么问题。非常感谢您的帮助。在分配任何内容之前,您正在使用变量
oper
:
if(equ.search(oper) !== -1){
oper = t["ops"][i];
未定义的值将转换为空正则表达式,因此它将始终返回匹配项,这就是第一个运算符工作的原因。在下一次迭代中,变量将被分配到错误的运算符
在使用它查找操作员之前,将操作员分配给它:
oper = t["ops"][i];
if(equ.search(oper) !== -1){
这是学习如何使用JS调试器的好机会!我会非常非常小心,不要让用户输入的任何东西接近像
eval()
@zerkms这样危险的东西。我在Chrome和Mozilla中都尝试过这样做,但除了我已经知道的以外,没有任何结果(即“eval()”函数试图计算“#”符号)。无论如何谢谢你@MikeW感谢您的建议,我计划创建一个函数来替换eval()
。谢谢@康纳·奥布莱恩:你说的“一无所获”是什么意思?放几个断点,一步一步地检查你的算法。谢谢!正是我需要的!
if(equ.search(oper) !== -1){
oper = t["ops"][i];
oper = t["ops"][i];
if(equ.search(oper) !== -1){