2021-07-21
1611
#vanilla javascript
Lawrence Eagles
58810
Jul 21, 2021 â‹… 5 min read

Understanding JavaScript decorators

Lawrence Eagles Senior full-stack developer, writer, and instructor.

Recent posts:

Solid Principles For Javascript

SOLID principles for JavaScript

SOLID principles help us keep code flexible. In this article, we’ll examine all of those principles and their implementation using JavaScript.

Frank Joseph
Dec 5, 2024 â‹… 10 min read
Master JavaScript Date And Time: From Moment.js To Temporal

Master JavaScript date and time: From Moment.js to Temporal

JavaScript’s Date API has many limitations. Explore alternative libraries like Moment.js, date-fns, and the new Temporal API.

Yan Sun
Dec 4, 2024 â‹… 9 min read
Npm Vs. Npx: What’s The Difference?

npm vs. npx: What’s the difference?

Explore use cases for using npm vs. npx such as long-term dependency management or temporary tasks and running packages on the fly.

Fimber Elemuwa
Dec 3, 2024 â‹… 5 min read
How To Audit And Validate AI-Generated Code Output

How to audit and validate AI-generated code output

Validating and auditing AI-generated code reduces code errors and ensures that code is compliant.

Boemo Mmopelwa
Dec 2, 2024 â‹… 5 min read
View all posts

5 Replies to "Understanding JavaScript decorators"

  1. Error in code there
    “Let’s learn about this by rewriting our log decorator:

    function log(target, name, descriptor) {
    if (typeof original === ‘function’) { <—– what is "original" ?

  2. The @log class method example is completely wrong and needs to be updated. The log function receives 3 arguments: the target object, the name of the target class member and a descriptor, an object containing the implementation of the class member.

    1. Hello A, it is important to note that the decorator API has evolved. You seem to refer to the stage 2 API. Below is the updated shape of the current sage 3 API in TypeScript:

      type Decorator = (value: Input, context: {
      kind: string;
      name: string | symbol;
      access: {
      get?(): unknown;
      set?(value: unknown): void;
      };
      private?: boolean;
      static?: boolean;
      addInitializer?(initializer: () => void): void;
      }) => Output | void;

      Checkout https://github.com/tc39/proposal-decorators for more.

Leave a Reply