Skip to content

Latest commit

 

History

History
30 lines (24 loc) · 982 Bytes

what_is_a_decorator.md

File metadata and controls

30 lines (24 loc) · 982 Bytes

What is a decorator?

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