JavaScript 注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17065949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript annotations
提问by Paul Verest
Are there JavaScript annotations?
有 JavaScript 注释吗?
Of course JavaScript doesn't have them, but are there additional libraries or proposed language extension, for example
当然 JavaScript 没有它们,但是否有额外的库或建议的语言扩展,例如
@type {folder.otherjsmodule.foo}
function(){
foo = folder.otherjsmodule.foo();
...
return foo;
};
回答by Benjamin Gruenbaum
Update: There now exists a proposalfor proper decorators in JavaScript. It's currently stage 1and you can use it in BabelJS and traceur.
更新:现在有一个关于JavaScript 中适当装饰器的提议。它目前是第一阶段,你可以在 BabelJS 和 traceur 中使用它。
Several libraries, like the mentioned before closure use annotations in comments, the closure compilereven 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.
与“显而易见”的答案不同 - 是的,有 JavaScript 注释,一些运行时支持它们。
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:
这将导致函数内部的严格模式执行。最近在 Mozilla 中,我们得到了:
(function(){
"use asm";
//code in asmjs
})();
Which will cause the code to run in asmjs mode, optimizing for transpiling.
这将导致代码以 asmjs 模式运行,优化转译。
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.
是的,虽然面向方面的编程和注解在 JS 中广泛不常见,但完全有可能编写一个库来接受一个函数,查看它的.toString
,找出这样的注解在哪里结束并执行相关代码,然后是函数的其余部分。
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.
创建一个执行此操作的库非常简单,它需要一些讨厌的代码(使用 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
});
回答by Christian
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.
我知道这是旧的。但是 ES2016 提供了装饰器(它在 Javascript 中是否称为装饰器)。制作自己的装饰器很容易,或者您可以扩展我的装饰器模块,让您轻松包装/注入自定义代码。