{"id":2380,"date":"2024-08-28T10:28:01","date_gmt":"2024-08-28T09:28:01","guid":{"rendered":"https:\/\/codeflarelimited.com\/blog\/?p=2380"},"modified":"2024-08-28T15:43:10","modified_gmt":"2024-08-28T14:43:10","slug":"javascript-decorators","status":"publish","type":"post","link":"https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/","title":{"rendered":"JavaScript Decorators: Adding Functionality to Objects"},"content":{"rendered":"\n<p>In modern JavaScript development, decorators have emerged as a powerful pattern for extending the functionality of objects and classes in a clean and maintainable way. While decorators are not yet a native part of JavaScript, they are widely used in frameworks like Angular and libraries like TypeScript to enhance the capabilities of functions, methods, and entire classes. This article will explore the concept of JavaScript decorators, how they work, and how you can use them to add functionality to objects in your applications.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What Are JavaScript Decorators?<\/h3>\n\n\n\n<p>A decorator is a function that takes an object or a class as an argument and returns a new object or class with added behavior. In essence, decorators wrap or augment existing objects, allowing you to add new methods, modify existing ones, or even change how an object or method behaves altogether.<\/p>\n\n\n\n<p>For example, you can use a decorator to add logging functionality to a method, so every time the method runs, it creates a log entry. Another common use involves enforcing access control by adding checks before executing a method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How Decorators Work<\/h3>\n\n\n\n<p>Although JavaScript does not natively support decorators as of now, you can implement them using higher-order functions or by leveraging libraries like TypeScript. In a typical decorator pattern:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create the Decorator Function<\/strong>: This function takes the target object (such as a class or method) and optionally additional arguments.<\/li>\n\n\n\n<li><strong>Modify or Extend the Target<\/strong>: Inside the decorator function, you can modify the target&#8217;s behavior, add properties or methods, or wrap it with additional functionality.<\/li>\n\n\n\n<li><strong>Return the New Object<\/strong>: The decorator returns a new object or class that includes the modified or extended behavior.<\/li>\n<\/ol>\n\n\n\n<p>Here\u2019s a simple example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">function logger(target, key, descriptor) {\n  const originalMethod = descriptor.value;\n\n  descriptor.value = function(...args) {\n    console.log(`Calling ${key} with`, args);\n    return originalMethod.apply(this, args);\n  };\n\n  return descriptor;\n}\n\nclass Example {\n  @logger\n  sayHello(name) {\n    return `Hello, ${name}!`;\n  }\n}\n\nconst example = new Example();\nexample.sayHello('World'); \/\/ Logs: Calling sayHello with ['World']\n<\/code><\/pre>\n\n\n\n<p>In this example, the <code>logger<\/code> decorator adds logging to the <code>sayHello<\/code> method, so every time the method is called, the arguments are logged to the console.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Use Cases for Decorators<\/h4>\n\n\n\n<p>Decorators offer versatility, serving a variety of scenarios effectively.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Logging<\/strong>: Automatically log method calls, arguments, and return values.<\/li>\n\n\n\n<li><strong>Memoization<\/strong>: Cache the results of expensive function calls to improve performance.<\/li>\n\n\n\n<li><strong>Validation<\/strong>: Ensure that method inputs meet certain criteria before execution.<\/li>\n\n\n\n<li><strong>Access Control<\/strong>: Restrict access to methods based on user roles or permissions.<\/li>\n\n\n\n<li><strong>Performance Monitoring<\/strong>: Track how long methods take to execute and identify bottlenecks.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>JavaScript decorators provide a powerful and flexible approach to enhancing objects and classes with additional functionality. Although they aren&#8217;t officially part of the JavaScript standard yet, developers frequently use them in modern development through various frameworks and libraries. By mastering decorators, you can write cleaner, more modular, and maintainable code, making your applications more robust and easier to extend.<\/p>\n\n\n\n<p><a href=\"https:\/\/codeflarelimited.com\/blog\/javascript-proxies\/\">JavaScript Proxies: Enhancing Object Behavior<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In modern JavaScript development, decorators have emerged as a powerful pattern for extending the functionality of objects and<\/p>\n","protected":false},"author":3,"featured_media":2384,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11],"tags":[99],"class_list":["post-2380","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-software-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JavaScript Decorators<\/title>\n<meta name=\"description\" content=\"Enhance your JavaScript skills by learning about JavaScript Decorators, a powerful tool for adding functionality to objects and classes, making your code cleaner and more maintainable.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Decorators\" \/>\n<meta property=\"og:description\" content=\"Enhance your JavaScript skills by learning about JavaScript Decorators, a powerful tool for adding functionality to objects and classes, making your code cleaner and more maintainable.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-08-28T09:28:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-08-28T14:43:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2024\/08\/IMG-20240828-WA0006.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1080\" \/>\n\t<meta property=\"og:image:height\" content=\"607\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Kene Samuel\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-decorators\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-decorators\\\/\"},\"author\":{\"name\":\"Kene Samuel\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/person\\\/c501609bab46c16807eb32106074f206\"},\"headline\":\"JavaScript Decorators: Adding Functionality to Objects\",\"datePublished\":\"2024-08-28T09:28:01+00:00\",\"dateModified\":\"2024-08-28T14:43:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-decorators\\\/\"},\"wordCount\":440,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-decorators\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/08\\\/IMG-20240828-WA0006.jpg\",\"keywords\":[\"software development\"],\"articleSection\":[\"javascript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-decorators\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-decorators\\\/\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-decorators\\\/\",\"name\":\"JavaScript Decorators\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-decorators\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-decorators\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/08\\\/IMG-20240828-WA0006.jpg\",\"datePublished\":\"2024-08-28T09:28:01+00:00\",\"dateModified\":\"2024-08-28T14:43:10+00:00\",\"description\":\"Enhance your JavaScript skills by learning about JavaScript Decorators, a powerful tool for adding functionality to objects and classes, making your code cleaner and more maintainable.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-decorators\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-decorators\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-decorators\\\/#primaryimage\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/08\\\/IMG-20240828-WA0006.jpg\",\"contentUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/08\\\/IMG-20240828-WA0006.jpg\",\"width\":1080,\"height\":607},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript-decorators\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"javascript\",\"item\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/javascript\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"JavaScript Decorators: Adding Functionality to Objects\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/\",\"name\":\"\",\"description\":\"Sustainable solutions\",\"publisher\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#organization\",\"name\":\"Codeflare Limited\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/codeflare.png\",\"contentUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/codeflare.png\",\"width\":1040,\"height\":263,\"caption\":\"Codeflare Limited\"},\"image\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/person\\\/c501609bab46c16807eb32106074f206\",\"name\":\"Kene Samuel\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3e1716cd715a5b5491e1f2da373b52f2f73aeb37d268baff34719116e386d848?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3e1716cd715a5b5491e1f2da373b52f2f73aeb37d268baff34719116e386d848?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3e1716cd715a5b5491e1f2da373b52f2f73aeb37d268baff34719116e386d848?s=96&d=mm&r=g\",\"caption\":\"Kene Samuel\"},\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/author\\\/kene\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Decorators","description":"Enhance your JavaScript skills by learning about JavaScript Decorators, a powerful tool for adding functionality to objects and classes, making your code cleaner and more maintainable.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Decorators","og_description":"Enhance your JavaScript skills by learning about JavaScript Decorators, a powerful tool for adding functionality to objects and classes, making your code cleaner and more maintainable.","og_url":"https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/","article_published_time":"2024-08-28T09:28:01+00:00","article_modified_time":"2024-08-28T14:43:10+00:00","og_image":[{"width":1080,"height":607,"url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2024\/08\/IMG-20240828-WA0006.jpg","type":"image\/jpeg"}],"author":"Kene Samuel","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/#article","isPartOf":{"@id":"https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/"},"author":{"name":"Kene Samuel","@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/person\/c501609bab46c16807eb32106074f206"},"headline":"JavaScript Decorators: Adding Functionality to Objects","datePublished":"2024-08-28T09:28:01+00:00","dateModified":"2024-08-28T14:43:10+00:00","mainEntityOfPage":{"@id":"https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/"},"wordCount":440,"commentCount":0,"publisher":{"@id":"https:\/\/codeflarelimited.com\/blog\/#organization"},"image":{"@id":"https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/#primaryimage"},"thumbnailUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2024\/08\/IMG-20240828-WA0006.jpg","keywords":["software development"],"articleSection":["javascript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/","url":"https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/","name":"JavaScript Decorators","isPartOf":{"@id":"https:\/\/codeflarelimited.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/#primaryimage"},"image":{"@id":"https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/#primaryimage"},"thumbnailUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2024\/08\/IMG-20240828-WA0006.jpg","datePublished":"2024-08-28T09:28:01+00:00","dateModified":"2024-08-28T14:43:10+00:00","description":"Enhance your JavaScript skills by learning about JavaScript Decorators, a powerful tool for adding functionality to objects and classes, making your code cleaner and more maintainable.","breadcrumb":{"@id":"https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/#primaryimage","url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2024\/08\/IMG-20240828-WA0006.jpg","contentUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2024\/08\/IMG-20240828-WA0006.jpg","width":1080,"height":607},{"@type":"BreadcrumbList","@id":"https:\/\/codeflarelimited.com\/blog\/javascript-decorators\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codeflarelimited.com\/blog\/"},{"@type":"ListItem","position":2,"name":"javascript","item":"https:\/\/codeflarelimited.com\/blog\/javascript\/"},{"@type":"ListItem","position":3,"name":"JavaScript Decorators: Adding Functionality to Objects"}]},{"@type":"WebSite","@id":"https:\/\/codeflarelimited.com\/blog\/#website","url":"https:\/\/codeflarelimited.com\/blog\/","name":"","description":"Sustainable solutions","publisher":{"@id":"https:\/\/codeflarelimited.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codeflarelimited.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codeflarelimited.com\/blog\/#organization","name":"Codeflare Limited","url":"https:\/\/codeflarelimited.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/codeflare.png","contentUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/codeflare.png","width":1040,"height":263,"caption":"Codeflare Limited"},"image":{"@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/person\/c501609bab46c16807eb32106074f206","name":"Kene Samuel","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3e1716cd715a5b5491e1f2da373b52f2f73aeb37d268baff34719116e386d848?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3e1716cd715a5b5491e1f2da373b52f2f73aeb37d268baff34719116e386d848?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3e1716cd715a5b5491e1f2da373b52f2f73aeb37d268baff34719116e386d848?s=96&d=mm&r=g","caption":"Kene Samuel"},"url":"https:\/\/codeflarelimited.com\/blog\/author\/kene\/"}]}},"jetpack_featured_media_url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2024\/08\/IMG-20240828-WA0006.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts\/2380","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/comments?post=2380"}],"version-history":[{"count":1,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts\/2380\/revisions"}],"predecessor-version":[{"id":2381,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts\/2380\/revisions\/2381"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/media\/2384"}],"wp:attachment":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/media?parent=2380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/categories?post=2380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/tags?post=2380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}