A decorator is a function that takes another function or method and adds additional functionality to it without modifying the original function. Decorators are commonly used in JavaScript to modify or enhance behavior in a reusable way.
Example:
function log(target, key, descriptor) {
const original = descriptor.value;
descriptor.value = function(...args) {
console.log('Calling ' + key);
return original.apply(this, args);
};
return descriptor;
}
class MyClass {
@log
myMethod() {
console.log('Method executed');
}
}
const obj = new MyClass();
obj.myMethod(); // Logs: 'Calling myMethod' and 'Method executed'
Tags: basic, JavaScript, design patterns
URL: https://www.tiktok.com/@jsmentoring/photo/7460446342461099296