JavaScript How can I use decorators today?

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?

Answer:1

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.

Answer:2

There are some solutions to use decorators:

  • babel - an es next to es5 compiler with support of decorators.
  • traceur - another es next to es5 compiler by google.
  • typescript - a typed superset of javascript language that supports decorators.

There are some difference how these tools transpile a "modern" javascript to an older one so you can explore it if needed as they have online playgrounds.

Answer:3

I'm totally new to eslint configurations. I'm using tslint for typescript for quite a while now so I'm familiar with the concept. eslint uses for many rules a syntax like "no-cond-assign": [2, "...

I'm totally new to eslint configurations. I'm using tslint for typescript for quite a while now so I'm familiar with the concept. eslint uses for many rules a syntax like "no-cond-assign": [2, "...

I have some problem when I try to upsert my object with new ones(parsed from xml file),but I got the following error: MongoError: exception: After applying the update to the document {_id: ObjectId(...

I have some problem when I try to upsert my object with new ones(parsed from xml file),but I got the following error: MongoError: exception: After applying the update to the document {_id: ObjectId(...

Our website is currently using jQuery library and getting a traffic of about 1 million monthly. We want to include API centric approach, so decided to move to Javascript MVC and have chosen angularJS ...

Our website is currently using jQuery library and getting a traffic of about 1 million monthly. We want to include API centric approach, so decided to move to Javascript MVC and have chosen angularJS ...

I have the following code : var fomattedDate = moment(myDate).format("L"); Sometimes moment(myDate).format("L") returns "Invalid date", I want to know if there is a way to prevent that and return an ...

I have the following code : var fomattedDate = moment(myDate).format("L"); Sometimes moment(myDate).format("L") returns "Invalid date", I want to know if there is a way to prevent that and return an ...