javascript 中的 typedef
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9753105/
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
Typedef in javascript
提问by Vivendi
Is it possible to typedef things in javascript somehow? Maybe by getting it from the prototype
object or something?
是否可以以某种方式在 javascript 中 typedef 东西?也许通过从prototype
对象或其他东西获取它?
For instance, i'd like to typedef the var
keyword.
例如,我想 typedefvar
关键字。
var string = prototype.var;
So now in stead of using 'var' i can use:
所以现在我可以使用:
string blaat = "It's like using the 'var' keyword";
Is this possible somehow in javascript?
这在javascript中有可能吗?
回答by Tomalak
No, this is not possible. Period.
不,这是不可能的。时期。
Your example is also an impossible thing to want, even though I understand the motivation. JavaScript is dynamically typed. You cannot declare variables to be string. And in that light the whole statement string x = "foo";
is pointless.
即使我理解动机,你的例子也是一件不可能的事情。JavaScript 是动态类型的。您不能将变量声明为字符串。从这个角度来看,整个声明string x = "foo";
毫无意义。
EDITYes, it's possible to achieve this effect with TypeScript. No, TypeScript is not JavaScript. The question was about the latter. That you can do a similar thing in a completely different programming language does not make this answer incorrect or obsolete.
编辑是的,可以使用 TypeScript 实现这种效果。不,TypeScript 不是 JavaScript。问题是关于后者。您可以用完全不同的编程语言做类似的事情并不会使这个答案不正确或过时。
Declaring a variable as, e.g., string
will remain impossible in JavaScript until the day when the ECMAScript Standard adds static typing to the language.
string
在 ECMAScript 标准将静态类型添加到语言中的那一天之前,将变量声明为,例如,在 JavaScript 中仍然是不可能的。
回答by gdoron is supporting Monica
You can not override or define keywords in javascript.
您不能在 javascript 中覆盖或定义关键字。
So, No, it can't be done.
所以,不,这是做不到的。
Regarding to your example, as @Lightness Races in Orbit commented, the example does not make sense as you don't have static typing in javascript(unlike java
, C#
etc')!
关于你的例子,如@Lightness种族在轨道评论,示例没有意义,你不必在JavaScript静态类型(不像java
,C#
等')!
var x = "12";
x = 12;
x = true;
x = function (){/*.../*};
All valid!
都有效!
so let's say you could define string
to be var
, will this make sense to you:
所以假设你可以定义string
为var
,这对你有意义吗:
string x = "12";
x = 12;
x = true;
x = function (){/*.../*};
There will be no errors, but ?!
不会有错误,但是?!
回答by Nicole Stein
This is not a feature of pure JavaScript, but it can be done if you're using Google's Closure Compiler, which allows you to precompile your JavaScript and check the types at compile time.
这不是纯 JavaScript 的功能,但如果您使用 Google 的 Closure Compiler 就可以完成,它允许您预编译 JavaScript 并在编译时检查类型。
So you could have
所以你可以有
/** @type {string} */
var str = "Lorem ipsum";
And if str
were not a string, you'd get a warning when you compile your code.
如果str
不是字符串,则在编译代码时会收到警告。
See https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compilerfor more information.
有关更多信息,请参阅https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler。