Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript annotations

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; }; 
like image 228
Paul Verest Avatar asked Jun 12 '13 12:06

Paul Verest


People also ask

What are annotations in TypeScript?

TypeScript - Type Annotations TypeScript is a typed language, where we can specify the type of the variables, function parameters and object properties. We can specify the type using :Type after the name of the variable, parameter or property. There can be a space after the colon.

What is an annotation?

Annotation is a written conversation between you and the writer in which you actively respond to the text. Pretend you are talking to the writer as you read. This exercise will help you to find connections between ideas in the text and ideas in other sources.

What is the purpose of annotations?

What is Annotating? Annotating is any action that deliberately interacts with a text to enhance the reader's understanding of, recall of, and reaction to the text. Sometimes called "close reading," annotating usually involves highlighting or underlining key pieces of text and making notes in the margins of the text.


1 Answers

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 }); 
like image 187
Benjamin Gruenbaum Avatar answered Sep 25 '22 10:09

Benjamin Gruenbaum