Create a module that keeps track of a score. It should have methods to add points using this code 'ScoreTracker.addPoints(10);'. Then, get the current score.
Design patterns are reusable solutions to common programming problems. They help write clean, maintainable, and efficient code.
Encapsulates related code into a single unit with private and public members.
Example:
const Counter = (function() {
let count = 0; // private variable
return {
increment: function() {
count++;
return count;
},
reset: function() {
count = 0;
}
};
})();
console.log(Counter.increment()); // 1
console.log(Counter.increment()); // 2
Counter.reset();
console.log(Counter.increment()); // 1
Singleton PatternEnsures a class has only one instance and provides a global point of access.