Keep Your Readers Reading

People like to read stories: stories about a person (including you) who experiences something, has an emotion about that experience, and the experience has consequences. Then they like to know what…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Function Decorations in Javascript

Functional programming (FP), in general, is the ability to create your code using pure functions, without any shared state between them. The concept of creating tiny, little baby functions which do one thing and one thing only, and reusing them again and again in the process of software development has seen a major jump in the last three to four years in industrial software development.

Here, we are going to talk about one of the core principles of functional programming : Function Decorations.

Now, before we jump into what function decoration actually is, you need to understand and remember a few key points. You need to have a clear understanding of what closures are, and also understand that you cannot edit functionalities inside of a function after it has been saved.

In layman terms, suppose you have a function which multiplies a given input by 2 and returns the output. Now during execution of the said function, you cannot magically edit the functionality and say that the input is multiplied by 3 or so.

Well, let’s just say that we can trick the reader into thinking we CAN edit a function during execution time (not actually edit the core function) by function decorations. We are going to understand this concept with a code example.

Here multiplyByTwo is our core function and oncify is a function which ‘edits’ the multiplyByTwo function to run only once.

Seeing from the code example, on line 15, if I asked you what is 10 in oncifyMultiplyByTwo(10), you would probably say its multiplyByTwo and that the 10 is passed in the multiplyByTwo function. Conceptually you would be right, but technically you would be 100% wrong. Let’s take code line by line for this one, exactly how a JS compiler and execution engine would do.

As you saw above how we “edited” or “decorated” the multiplyByTwo function to run on some condition during runtime and not actually edit the core function definition by any means. This is one of the truest, rawest power of FP in Javascript. This practice can be used to create some seriously modular and powerful pure functions, and you might encounter some real life cases where FP can prove valuable to you.

Add a comment

Related posts:

How Overcoming Addiction Makes You Stronger

In our Kim household, Oreos are a permanent staple in the kitchen pantry, as we routinely replace them even before the ginormous Costco packs are completely empty, thanks to Costco! Remember not too…

How artificial intelligence leads to changes in marketing and consumption behavior

In the open battle for consumers’ attention, A.I. can be a highly affective tool for companies not only to analyze customer data, but also to raise interest for certain products or services. (Every…

Now Or Never.

China is moving ahead full-speed on AI. Nobody can stop them — everybody can see the future, and it’s Chinese, very quickly. Now is the only shot to make this into a new world order, a planetary…