53

Are there JavaScript annotations?

Of course JavaScript doesn't have them, but are there additional libraries or proposed language extension, for example

@type {folder.otherjsmodule.foo}
function(){
    foo = folder.otherjsmodule.foo();
    ...
    return foo;
};
5
  • 1
    google closure and closure compiler uses annotations to check types and make sure variables are private.
    – HMR
    Jun 12, 2013 at 12:53
  • 1
    If your goal is automatic documentation generation have a look at JSDoc. Jun 12, 2013 at 12:53
  • "...proposed language extension..." - Proposed by whom? I'm sure somebody has proposed it.
    – nnnnnn
    Jun 12, 2013 at 12:58
  • @nnnnnn Then name it and give link. JavaScript language extension may come from big companies like Microsoft with their TypeScript and ECMAScript 6 (but that change feels like really new language). Additionally there may be less intrusive solutions like some utils that can help in some cases. JSDoc was already mentioned. Jun 12, 2013 at 13:53
  • Please skype me pverest Dec 17, 2013 at 9:36

2 Answers 2

36

Update: There now exists a proposal for proper decorators in JavaScript. It's currently stage 1 and you can use it in BabelJS and traceur.


Several libraries, like the mentioned before closure use annotations in comments, the closure compiler even asserts types as much as it can in compile time. However, these are not actual 'annotations' in the classical sense.

Unlike the 'obvious' answer - Yes, there are JavaScript annotations, some run-times support them.

For example

(function(){
    "use strict";
    //code in strict mode
})();

This will cause strict mode execution inside the function. More recently in Mozilla we've gotten:

(function(){
    "use asm";
    //code in asmjs
})();

Which will cause the code to run in asmjs mode, optimizing for transpiling.

Can I use these sort of annotations in my library?

Yes, while aspect oriented programming and annotations are widely uncommon in JS, it's perfectly possible to write a library that accepts a function, looks at its .toString, figures out where such annotations end and executes the relevant code and then the rest of the function.

For example

an(function(){
    "validate user"; // this could be something you implement yourself
    "use strict";
})();

Creating a library that does this is pretty simple, it would require some nasty code (using a Function constructor and parsing the function as a string) but it's certainly possible. It's even debuggable in the new dev-tools and it's almost as fast as a native function.

Proposed syntax could be:

an.add("assertEmail",function(x){
    if(!emailRegex.test(x){
        throw new Error("Invalid call to function - expected email got",x);
    }
});

// later on 

an(function subscribeToNewsLetter(x){
    "assertEmail";
    var xhr = new XMLHttpRequest();
    //send email
});
3
  • 3
    "use strict" like have been called compiler directives. Jul 10, 2013 at 8:57
  • 1
    "acceptEmail" is macros. Can you reference any sources that call such thing as javaScript annotations? Jul 10, 2013 at 8:58
  • Thx for the heads up, just wrote something like that: github.com/pedroassis/js-annotation-reader
    – pedroassis
    Jun 22, 2015 at 22:48
14

I know this is old. But ES2016 provides decorators (is it called decorators in Javascript). It is easy to make your own decorators, or you can extend my decorator module that let you easily wrap/inject your custom code.

https://github.com/cmartin81/decorator-wrap

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.