国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Javascript實現運算符重載詳解

2024-05-06 15:20:08
字體:
來源:轉載
供稿:網友

最近要做數據處理,自定義了一些數據結構,比如Mat,Vector,Point之類的,對于加減乘除之類的四則運算還要重復定義,代碼顯得不是很直觀,javascript沒有運算符重載這個像C++、C#之類的功能的確令人不爽,于是想“曲線救國”,自動將翻譯代碼實現運算符重載,實現思路其實很簡單,就是編寫一個解釋器,將代碼編譯。例如:

S = A + B (B - C.fun())/2 + D

翻譯成

`S = replace(replace(A, '+', replace(replace(B,'',(replace(B,'-',C.fun())))),'/',2),'+',D)`

在replace函數中我們調用對象相應的運算符函數,replace函數代碼如下:

/** * 轉換方法 * @param a * @param op * @param b * @returns {*} * @private */export function __replace__(a,op,b){  if(typeof(a) != 'object' && typeof(b) != 'object'){    return new Function('a','b','return a' + op + 'b')(a,b)  }  if(!Object.getPrototypeOf(a).isPrototypeOf(b)    && Object.getPrototypeOf(b).isPrototypeOf(a)){    throw '不同類型的對象不能使用四則運算'  }  let target = null  if (Object.getPrototypeOf(a).isPrototypeOf(b)) {    target = new Function('return ' + b.__proto__.constructor.name)()  }  if (Object.getPrototypeOf(b).isPrototypeOf(a)) {    target = new Function('return ' + a.__proto__.constructor.name)()  }  if (op == '+') {    if (target.__add__ != undefined) {      return target.__add__(a, b)    }else {      throw target.toString() +'/n未定義__add__方法'    }  }else if(op == '-') {    if (target.__plus__ != undefined) {      return target.__plus__(a, b)    }else {      throw target.toString() + '/n未定義__plus__方法'    }  }else if(op == '*') {    if (target.__multiply__ != undefined) {      return target.__multiply__(a, b)    }else {      throw target.toString() + '/n未定義__multiply__方法'    }  } else if (op == '/') {    if (target.__divide__ != undefined) {      return target.__divide__(a, b)    }else {      throw target.toString() + '/n未定義__divide__方法'    }  } else if (op == '%') {    if (target.__mod__ != undefined) {      return target.__mod__(a, b)    }else {      throw target.toString() + '/n未定義__mod__方法'    }  } else if(op == '.*') {    if (target.__dot_multiply__ != undefined) {      return target.__dot_multiply__(a, b)    }else {      throw target.toString() + '/n未定義__dot_multiply__方法'    }  } else if(op == './') {    if (target.__dot_divide__ != undefined) {      return target.__dot_divide__(a, b)    }else {      throw target.toString() + '/n未定義__dot_divide__方法'    }  } else if(op == '**') {    if (target.__power__ != undefined) {      return target.__power__(a, b)    }else {      throw target.toString() + '/n未定義__power__方法'    }  }else {    throw op + '運算符無法識別'  }}

replace實現非常簡單,不做過多解釋,重要的部分是如何實現代碼的編譯。大學學習數據結構時四則運算的實現就是這翻譯的基礎,略微有些差異。簡單描述一下流程:

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 襄汾县| 东乌珠穆沁旗| 怀远县| 淳安县| 皋兰县| 旺苍县| 思南县| 北海市| 绥棱县| 景洪市| 太谷县| 政和县| 河东区| 旬邑县| 青铜峡市| 扶余县| 文山县| 江陵县| 广水市| 姚安县| 襄樊市| 牟定县| 祁东县| 石阡县| 峨山| 子洲县| 泾川县| 专栏| 云阳县| 前郭尔| 公主岭市| 大宁县| 玉树县| 沧源| 靖江市| 绥中县| 二手房| 平原县| 资阳市| 偏关县| 乌什县|