开发者

what does @const mean in JavaScript?

开发者 https://www.devze.com 2023-03-13 20:43 出处:网络
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

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 a开发者_运维问答nnotation 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 @const annotation.

/**  
* 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 const keyword, 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 :)


Looks like it's referring to the use of @const in JSDoc (Think JavaDoc) to mark certain objects constant.

It's not a javascript keyword and not a part of the language – just something used in inline documentation in JSDoc.

Edit: The mention of 'never use the const keyword' may confuse you. The guidelines tell not to use const because IE doesn't support it, instead marking the "constants" constant in the documentation.

Some javascript compiler might parse JSdoc for notation like that, but I doubt it's standard.

0

精彩评论

暂无评论...
验证码 换一张
取 消