SOURCE

class CalcEval {
//复杂的计算方法(包含括号)
complexEval = function(str) {
    if (str == undefined) {
        return "";
    }
    if (typeof str != "string") {//转化成字符串
        str = str + ""
    }
      var multObj = CalcEval.matchBracket(str);//匹配括号
      //不断计算最底层括号的数据
      while (null != multObj)
      {
         var content = multObj[0] +"";
         
        var result = this.simpleEval(content.substr(1,content.length-2));
        str = str.replace(multObj[0],result);
        multObj = CalcEval.matchBracket(str);
      }
      
    return this.simpleEval(str);
}
//简单的计算方法,只有加减乘除
static matchBracket = function(str) {
  return str.match(/([\()]∗?)([\()]∗?)/g);
}

simpleEval = function(str) {
    if (str == null) {
        return "";
    }
    if (typeof str != "string") {//转化成字符串
        str = str + ""
    }
    
    var valueArray = new Array();//值的数组
    var markArray = new Array();//符号的数组
    var tempValue = "";
    var ch = str.split("");  
    var isOper = false; 
    for(var i=0; i< ch.length; i++){
        if( ch[i] == "+" || ch[i] == "-" || ch[i] == "*" || ch[i] == "/") {//符号
                var dv = tempValue*1;  
                if(isOper){  
                    var value = valueArray.pop();  
                    var mark = markArray.pop();  
                    dv = this.simpleTwoEval(mark, value, dv);  
                }  
                valueArray.push(dv);  
                markArray.push(ch[i]);  
                tempValue = "";  
                isOper = false;  
                if( ch[i] == "*" || ch[i] == "/" )
                    isOper = true;  
            }else{
                tempValue += ch[i] + "";  
                if(i == ch.length -1){//最后一位
                    var dv = tempValue*1;  
                    if(isOper){  
                        var dv1 = valueArray.pop();  
                        var mark = markArray.pop();  
                        var result = this.simpleTwoEval(mark, dv1, tempValue);  
                        dv = result;  
                    }  
                    valueArray.push(dv);  
                }  
            } 
        }
        
        valueArray = this.reverseArray(valueArray);  
        markArray =  this.reverseArray(markArray);  
        while(valueArray.length > 1){  
            var v1 = valueArray.pop();  
            var v2 = valueArray.pop();  
            var mark = markArray.pop();  
            valueArray.push(this.simpleTwoEval(mark, v1, v2));  
        }  
        return valueArray[0];  
    
}

//两个数的加减乘除
simpleTwoEval = function(mark,value1,value2){
  let res = 0;
  switch (mark) {
    case "+": res = value1 + value2; break;
    case "-": res = value1 - value2; break;
    case "*": res = value1 * value2; break;
    case "/": res = value1 / value2; break;
    default: res = 0;
  }
  return res;
}

//反转数组
reverseArray = function(oldArray){
    var newArray = new Array();
    var size = oldArray.length;
    for(var i=0; i< size; i++){
        newArray.push(oldArray.pop());  
    }
    return newArray;  
    
}

}
var test = new CalcEval;
var a = test.complexEval(1+3)
console.log(a)

console 命令行工具 X clear

                    
>
console