2012-04-19 74 views
1

我繼承了一個JavaScript代碼庫,我是JavaScript新手。所以我使用JSHint來避免常見錯誤,誤用。如何避免這種邪惡eval的情況?

JSHint發現這段代碼,但我不知道如何避免邪惡的eval:

function GetProperties(object) { 
    var result, property, t; 
    result = ''; 
    for (property in object) { 
     if (property.indexOf('Get', 0) === 0) { 
      t = object[property] + "..."; 
      eval("if (GetNumOfParameter(t) == 0) var m = object." + property + "(); else var m = -100;"); 

      if (window.m != -100) { 
       result += property + ': ' + window.m + '\r\n'; 
      } 
     } 
    } 
    return result; 
} 
+3

'var m = object [property]()'也許? – 2012-04-19 14:28:41

回答

2

使用下,它是更好的,你不需要使用m如果你不」不要在其他地方使用它。

function GetProperties(object) { 
    var result, property, t; 
    result = ''; 
    for (property in object) { 
     if (property.indexOf('Get', 0) === 0) { 
      t = object[property] + "..."; 

      if (GetNumOfParameter(t) == 0) 
       result += property + ': ' + object[property]() + '\r\n'; 
     } 
    } 
    return result; 
} 
+0

而不是(例如)'GetNumOfParameter(t)== 0',你可以簡單地'!GetNumOfParameter(t)' – Zirak 2012-11-26 13:58:23

相關問題