2014-10-07 176 views
0

因此,我設計的代碼將使用戶能夠創建僞特定操作,可以使用特殊的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; 
} 

測試#1

var hash = new Operator("#","x # y = 3 * x - y"); 
var dash = new Operator("q","x q y = y"); 

activate(dash); 
activate(hash); 

console.log(cmd.eval("3 q -2")); // RETURNS -2 
console.log(cmd.eval("3 # -2")); // RETURNS NOTHING 

測試#2

var hash = new Operator("#","x # y = 3 * x - y"); 
var dash = new Operator("q","x q y = y"); 

activate(hash); // HASH IS CALLED FIRST THIS TIME 
activate(dash); 

console.log(cmd.eval("3 q -2")); // RETURNS NaN 
console.log(cmd.eval("3 # -2")); // RETURNS 11 

我已經解決此事情了大約一個小時,我不知道發生了什麼事情錯了。幫助是高度讚賞。

+3

這是一個很好的機會,學習如何使用JS調試器! https://developer.chrome.com/devtools/docs/javascript-debugging – zerkms 2014-10-07 22:06:49

+1

我會非常非常謹慎地允許用戶輸入任何與eval()一樣危險的東西。 – 2014-10-07 22:12:00

+0

@zerkms我試着在Chrome和Mozilla中做這件事,除了我已經知道的東西(即'eval()'函數試圖評估'#'符號)之外不會產生任何結果。不管怎麼說,多謝拉! – 2014-10-07 22:14:14

回答

1

在這裏,您使用的是可變oper已分配任何東西之前到它:

if(equ.search(oper) !== -1){ 
    oper = t["ops"][i]; 

undefined值將被轉換成一個空的正則表達式,所以它總是返回匹配,這就是爲什麼第一操作員工作。在下一次迭代中,變量將被分配錯誤的運算符。

用它來尋找運營商之前分配運算符將其:

oper = t["ops"][i]; 
if(equ.search(oper) !== -1){ 
+0

謝謝!正是我需要的! – 2014-10-07 22:25:40