Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use decorators today?

Tags:

I see decorators being used today already in some javascript code. My question is really two fold.

First:

If decorators have not even been finalized how is it possible to use them in production code, today? Won't browser support be non-existent?

Second:

Given it is possible to use it today, as some open source projects would suggest, what's a typically recommended setup for getting decorators to work?

like image 830
Jacob Baris Avatar asked Dec 25 '15 09:12

Jacob Baris


People also ask

How useful are decorators?

You'll use a decorator when you need to change the behavior of a function without modifying the function itself. A few good examples are when you want to add logging, test performance, perform caching, verify permissions, and so on. You can also use one when you need to run the same code on multiple functions.

What are decorators and what is their usage?

A decorator in Python is a function that takes another function as its argument, and returns yet another function . Decorators can be extremely useful as they allow the extension of an existing function, without any modification to the original function source code.

Where can decorators be applied to?

The class decorator is applied to the constructor of the class and can be used to observe, modify, or replace a class definition. A class decorator cannot be used in a declaration file, or in any other ambient context (such as on a declare class).

Can I use decorator JS?

You may apply decorators to class fields, methods, and even the whole class. However, you can't use decorators on plain JavaScript objects; they only work with classes.


1 Answers

You're right, ES2016 decorators are not yet part of the spec. But it doesn't mean we can't use it today.

First let's take a step back and go over "what is a decorator". Decorators are simply wrappers that add behavior to an object. It's not a new concept in javascript (or programming in general), it's actually been around for a while...

Here's a basic example of a decorator that checks permissions:

function AuthorizationDecorator(protectedFunction) {     return function() {         if (user.isTrusted()) {             protectedFunction();         } else {             console.log('Hey! No cheating!');         }     } } 

Using it would look like this:

AuthorizationDecorator(save); 

You see all we're doing is simply wrapping up some other function. You can even pass a function through multiple decorators each adding a piece of functionality or running some code.

You can even find some old articles explaining the decorator pattern in javascript.

Now that we understand decorators are actually something we (javascript community) were always able to do, it probably comes as no shock that really when we utilize ES2016 decorators today they are simply just being compiled down to ES5 code hence why you maintain browser compatibility. So for the time being it is simply syntactic sugar (some really sweet sugar I might add).

As for which compiler to use to convert your ES2016 code to ES5 code, you have some choices: Babel and Traceur are the most popular.

Here's further reading on Exploring ES2016 Decorators.

like image 85
Kirill Fuchs Avatar answered Sep 21 '22 08:09

Kirill Fuchs