可以使用闭包的方式实现让一个函数只执行一次,例如:
function once(fn) {
let hasBeenCalled = false;
let result;
return function() {
if(!hasBeenCalled) {
hasBeenCalled = true;
result = fn.apply(this, arguments);
}
return result;
};
}