skip to Main Content

According to Webpack documentation:

It can be put in front of function calls to mark them as side-effect-free. Arguments passed to the function are not being marked by the annotation and may need to be marked individually.

Can we write PURE function at declaration ? Something like:

var max = /*#__PURE__*/(a,b) => { return (a>b)?a:b }

If yes, what is the right syntax?

2

Answers


  1. As far as I know, there is no way you can declare function as pure in JavaScript.

    TypeScript has a proposal to introduce pure keyword for functions and methods, but currently this is just a draft proposal. You can read the whole thread, if you want here. The thread contains a comment with draft proposal description. When and whether at all this idea will be implemented is very hard to say.

    Login or Signup to reply.
  2. Like Bogdan Gishka noted, there is no (formal) way to declare a function as pure. Maybe it’s an idea to use a Symbol for it?

    const pure = Symbol('pure');
    const max = (a,b) => a > b ? a : b;
    max[pure] = true;
    
    console.log(`max(21,1): ${max(21, 1)}; max[pure]: ${max[pure]}`);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search