gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:20:13 28 4
gpt4 key购买 nike

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

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

最佳答案

您可以尝试使用代理进行日志记录

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" */

关于javascript - 添加日志以存储有关用户操作 JavaScript 的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60899465/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com