InterviewHubPRO
Section Overview
Q244

Question 244 of 521

Part 4: Prototypes, Object Methods & Event Loop (174 - 253)

47%
JavaScript Internals (521 Qs)•Part 4: Prototypes, Object Methods & Event Loop (174 - 253)

What is a decorator

A decorator is an expression that evaluates to a function and that takes the target, name, and decorator descriptor as arguments. Also, it optionally returns a decorator descriptor to install on the target object. Let's define admin decorator for user class at design time,

javascript
  function admin(isAdmin) {
     return function(target) {
         target.isAdmin = isAdmin;
     }
  }

  @admin(true)
  class User() {
  }
  console.log(User.isAdmin); //true

   @admin(false)
   class User() {
   }
   console.log(User.isAdmin); //false