添加日志以存储有关用户操作 JavaScript 的信息

我为此创建了一个函数。也许存在更好的解决方案?


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);

}


波斯汪
浏览 80回答 1
1回答

达令说

您可以尝试使用代理进行日志记录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" */
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript