We are considering adopting the Google JavaScript coding guidelines within our company to maintain consistency across projects but one thing is confusing me. In the section on constants it says to use the @const keyword annotation for compile-time constant enforcement but I have never come across the @ symbol before. Is this a Google extension or part of the core language?
Here is the full text:
For non-primitives, use the
@constannotation.
/**
* The number of seconds in each of the given units.
* @type {Object.<number>}
* @const
*/
goog.example.SECONDS_TABLE = {
minute: 60,
hour: 60 * 60
day: 60 * 60 * 24
}
This allows the compiler to enforce constant-ness.
As for the
constkeyword, Internet Explorer doesn’t parse it, so don’t use it.
JSDoc annotations as this one can be used to give hints to the Google Closure Compiler as described in this page. Again, this is not part of the language, and the use of @constant (beside adding some information for your fellow programmer) is relevant only if you use the closure compiler in your project.
Hope this will help 🙂