JS Coding Questions Logo
JS Coding Questions
#240💼 Interview💻 Code

What is a decorator

Advertisement

728x90

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
1function admin(isAdmin) {
2  return function(target) {
3  target.isAdmin = isAdmin;
4  }
5  }
6
7  @admin(true)
8
9class User() {
10}
11
12console.log(User.isAdmin); //true
13
14 @admin(false)
15
16class User() {
17}
18
19console.log(User.isAdmin); //false

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 69

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
240of476