Which method would you use to ensure a callback executes exactly once, regardless of how many times a function is called?
JavaScript doesn't have a built-in Function.prototype.once() method, so you would need to implement this functionality manually. This pattern is often called a 'once function' and is useful when you need to ensure that a callback executes only once, such as for initialization code or cleanup operations. Libraries like Lodash provide a _.once() utility for this purpose. A simple implementation could use closure to track whether the function has been called already:
```javascript
function once(fn) {
let called = false;
return function(...args) {
if (!called) {
called = true;
return fn.apply(this, args);
}
};
}
```