JavaScript的注解 [英] JavaScript annotations

查看:142
本文介绍了JavaScript的注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有JavaScript的注解?

当然JavaScript没有他们,但是否有额外的库或建议语言扩展,例如:

  @type {} folder.otherjsmodule.foo
功能(){
    富= folder.otherjsmodule.foo();
    ...
    返回foo的;
};


解决方案

更新:现在有一个存在的 JavaScript中适当的装饰建议。这是目前阶段1 ,您可以在BabelJS和traceur使用它。


若干库,就像在评论关闭使用注解前面提到的,关闭编译器甚至断言类型一样,因为它可以在编译的时候。然而,这些都不是传统意义上的实际的注释。

与'明显'的答案 - 是的,有JavaScript的注解,一些运行时支持它们

例如

 (函数(){
    使用严格的;
    // code严格模式
})();

这将导致函数内部严格执行模式。最近在Mozilla,我们已经得到了:

 (函数(){
    使用ASM
    // code在asmjs
})();

,这将导致code键中asmjs模式下运行,优化transpiling

我能否使用这些类注释在我的图书馆?

是的,而面向方面的编程和注解广泛少见JS,这是完全可以编写接受函数库,看它的的ToString ,计算出其中,这种注释结束,并执行相关的code和那么函数的其余部分。

例如

 的(函数(){
    验证用户; //这可能是你的东西自己实现
    使用严格的;
})();

创建,这是否是pretty简单的图书馆,它需要一些讨厌code(使用函数构造函数和解析函数的字符串),但它当然有可能。它甚至可调试在新的开发工具,它几乎一样快,本机的功能。

建议的语法是:

  an.add(assertEmail功能(X){
    如果(!emailRegex.test(X){
        抛出新的错误(到函数调用无效 - 预期电子邮件得了,X);
    }
});// 稍后的一个(函数subscribeToNewsLetter(X){
    assertEmail;
    VAR XHR =新XMLHtt prequest();
    //发电子邮件
});

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;
};

解决方案

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
});

这篇关于JavaScript的注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆