我是一个JavaScript的新手,我觉得强大的类型我的函数参数的不可抗拒的需要我编写的几个工具:
经过一些谷歌搜索,我想这不是直接可能的.但是,有没有通用的工具可以简单地模仿这个?
你的想法是什么?
小智 11
写"你不应该使用它"的人是错的.在下一个Java Script 2.x规范中,有一个计划来添加强类型变量.
同时,您可以使用非常简单的解决方案来模拟强类型:
var = Object.create( String );
Run Code Online (Sandbox Code Playgroud)
之后,在很多IDE(包括IntelliJ IDEA)中自动完成将很有效,并且您已声明并初始化了指定类型的对象.
了解更多关于我的博客.
Daf*_*aff 10
不,你不能,即使有一种方法,你也不应该.JavaScript是一种动态类型语言.但是,对于自动完成,您可以使用JSDoc样式文档标记来提供某些类型指针:
var Person = {
/**
* Say hi
* @param {String} name The name to say hi to
* @return {String}
*/
sayHi : function(name)
{
return 'Hi ' + name;
}
}
Run Code Online (Sandbox Code Playgroud)
如果使用它们完全取决于您的IDE.
Have you looked at Typescript? Its an open source project by Microsoft that allows you to develop using strong typing and then compiles the code into Javascript. I know is Microsoft but take a look before you dismiss it.
http://www.typescriptlang.org/
Edit 2017
There are now two big players on this scene, Typescript (as suggested above) has been battle proven and is now used extensively by Angular 2. If structure and fairly rigid typing if what you are looking for, that is your best bet.
Another option is Flow (https://flow.org/) it was developed by Facebook and is used by them heavily in React. Flow allows you to only specify which files you want to type check and is a lower barrier to entry IMO.
It is worth saying that adding type checking adds a fair amount of complexity to your build process - it requires you to have a build process!
| 归档时间: |
|
| 查看次数: |
14657 次 |
| 最近记录: |