Design Patterns · Middle · Question 35 of 75
What is the Null Object pattern?
const noopLogger = { info() {}, error() {} };
function createService(logger = noopLogger) { /* ... */ }- ✓Correct answer: A stub with the same interface instead of
nullchecks - 2An object whose properties are all
nullby default - 3A way to remove an object from memory by assigning
nullto it
Explanation
Instead of if (logger) logger.info(...) all over the code, a safe stub is passed in. This simplifies the code and eliminates "cannot read property of null" errors.