Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of JavaScript decorators?

I googled about JavaScript decorators but I'm not sure what the difference between calling a function using a decorator and calling a function normally is.

function myFunction(text) {
  console.log(text)
}

myFunction() vs @myFunction vs @myFunction()

I have a feeling I'm more than wrong here. Can someone explain?

like image 696
APixel Visuals Avatar asked Aug 13 '18 13:08

APixel Visuals


People also ask

What is a function decorator in JavaScript?

A decorator (also known as a decorator function) can additionally refer to the design pattern that wraps a function with another function to extend its functionality. This concept is possible in JavaScript because of first-class functions — JavaScript functions that are treated as first-class citizens.

What is a readonly decorator in JavaScript?

Have a look at readonly fuction. A decorator is nothing but a function that is called when the JavaScript runtime encounters the decorator. The target argument value of this function is an object representation of the entity onto which the decorator was added, which is getFullName method in this case.

What is the difference between Python and JavaScript decorators?

Unlike python where decorators can be used for both functions and classes, Here is Javascript Decorators are only be used on top of the Class. The decorator feature is still in the experimental phase and has not yet been released.

What is target and descriptor in JS decorator?

JS decorator functions are passed three arguments: 1 target is the class that our object is an instance of. 2 key is the property name, as a string, that we’re applying the decorator to. 3 descriptor is that property’s descriptor object.


2 Answers

Decorators are there to enable separation of concerns. Consider the following function:

function add(a, b) {
  return a + b;
}

Which is all well and dandy. But let's say that you want to add logging:

function add(a, b) {
  const sum = a + b;
  console.log(a, b, sum);
}

Now the function is longer and more complicated and conflates two things (summing and logging) that really don't have any relation. A better way to do it would be this:

function logs(f) {
  return function(...args) {
    const result = f(...args);
    console.log(args, result);
    return result;
  };
};

const addAndLog = logs(add);

And that's what a decorator is. What you're talking about (i.e. the JavaScript decorator proposal) is just a syntactic shortcut for the pattern above. Here logs is a decorator that adds a capability to the passed in function, the ability to log without having to complicate the function with a lot of extraneous code. The proposal is at the moment restricted to classes and methods, but I find it conceptually easier to explain with normal functions.

like image 137
Jared Smith Avatar answered Oct 30 '22 10:10

Jared Smith


Decorators are used to literally decorate a function.

Let's say you want to type your own decorator which can be used to see how much time a function needs to run. You could write a decorator @time() that does just that. When you are done, you can use this decorator before every function you want to track.

Decorators are used as high-order functions, mainly to have a functional composition of your code!

A nice example would be a @Component() decorator in Angular. Whilst using this decorator before your class, Angular knows it has to handle it as a component and does a couple of methods with it behind the scenes.

like image 26
Bertijn Pauwels Avatar answered Oct 30 '22 09:10

Bertijn Pauwels