Javascript Decorator Functions

You might be wondering why you’d want to decorate a function? Allow me to explain…

Decorator Functions are simply functions that have an object input and output a new “decorated” object.

So imagine a boring object with very few keys, values, methods, etc…once it enters the magical Decorator Function it will be “decorated” with all kinds of cool new key/values and/or methods.

What’s a method? Methods are just functions that are within objects, ie. it’s a value of a key. So for example, you might have an object key called ageCalculator and it would be assigned to a value which is a function that calculates your age.

There are some nuances that you must learn when dealing with Decorator Functions and I’ll just briefly touch on a few…

Firstly, here’s an example of a decorator function:

var decoratePerson = function(personObj, birthYear, name, hobby) {

personObj.birthYear = birthYear; = name;
personObj.hobby = hobby;

return personObj;


In the above example, the decorator function simply takes in an object as the first parameter along with several arguments that will be used to “decorate” the object. Then finally the new decorated object is returned.

Hope that helped give a quick intro to basic decorator functions!

Like what you read? Give Ali Bhatti a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.