<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bjorn-Donald Bassey</title>
    <description>The latest articles on DEV Community by Bjorn-Donald Bassey (@6lackbjorn).</description>
    <link>https://dev.to/6lackbjorn</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F667582%2Fb9b7c6f5-e037-43e0-9089-e4de7d4d3ebb.jpg</url>
      <title>DEV Community: Bjorn-Donald Bassey</title>
      <link>https://dev.to/6lackbjorn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/6lackbjorn"/>
    <language>en</language>
    <item>
      <title>Have you heard of function decorators</title>
      <dc:creator>Bjorn-Donald Bassey</dc:creator>
      <pubDate>Sat, 02 Jul 2022 17:41:51 +0000</pubDate>
      <link>https://dev.to/6lackbjorn/have-you-heard-of-function-decorators-36ee</link>
      <guid>https://dev.to/6lackbjorn/have-you-heard-of-function-decorators-36ee</guid>
      <description>&lt;p&gt;One advice we usually hear as developers is to do our very best to not repeat code. So what we do is place code which could be used numerous time in its separate function. But sometimes, it isn't so simple. Take try catch blocks for example,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;try {&lt;br&gt;
  console.log("loading...")&lt;br&gt;
  // run some code&lt;br&gt;
  console.log("Finished.")&lt;br&gt;
} catch (error) {&lt;br&gt;
  console.log(error)&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;JavaScript does have allocation for decorators. But there are only available for class methods and class fields. You can read more &lt;a href="https://medium.com/@victortoschi/using-decorators-in-javascript-e80674e4c6fa"&gt;here&lt;/a&gt;.&lt;br&gt;
But for now, we will be taking a look at a function decorators. A way to have a decorators for functions outside a class is to use higher function. This is a function that returns a function. This could be designed like a function wrapper&lt;/p&gt;

&lt;p&gt;Say you have a function&lt;br&gt;
&lt;code&gt;function printText(text){&lt;br&gt;
  console.log(&lt;/code&gt;Code says, ${text}&lt;code&gt;);&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And you want to wrap this function code with the try catch block from above. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;function loggingDecorator(wrappedFn) {&lt;br&gt;
  return function() {&lt;br&gt;
    try {&lt;br&gt;
      console.log("loading...")&lt;br&gt;
      // run some code&lt;br&gt;
      console.log("Finished.")&lt;br&gt;
    } catch (error) {&lt;br&gt;
       console.log(error)&lt;br&gt;
    }&lt;br&gt;
  }&lt;br&gt;
}&lt;/code&gt;&lt;br&gt;
You use the decorator like this&lt;br&gt;
&lt;code&gt;const wrapped = loggingDecorator(printText);&lt;br&gt;
wrapped("have a good day.");&lt;/code&gt;&lt;br&gt;
Thanks for reading.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
  </channel>
</rss>
