我为此创建了一个函数。也许存在更好的解决方案?function writeLog(operationIdentifier, prevResult, operationNumber, newResult) {const logEntry = { operation: operationIdentifier, prevResult: prevResult, number: operationNumber, result: newResult,};logEntries.push(logEntry);console.log(logEntries);由此:function add() { const enteredNumber = getUserNumberInput(); const initialResult = currentResult; currentResult += enteredNumber; createAndWriteOutput('+', initialResult, enteredNumber); writeLog('ADD', initialResult, enteredNumber, currentResult);}
1 回答
达令说
TA贡献1821条经验 获得超6个赞
您可以尝试使用代理进行日志记录
const increment = a => a + 1;
const proxy = new Proxy(increment, {
apply(target, thisArg, args) {
console.log(`Incrementing the value "${args[0]}"`);
const result = target(...args);
console.log(`Result: "${result}"`);
return result;
},
});
increment(5);
/* nothing logs here */
proxy(5);
/* Incrementing the value "5".
Result: "6" */
添加回答
举报
0/150
提交
取消