简体   繁体   English

Javascript 101-传递给函数的类型转换变量

[英]Javascript 101 - typecast variables passed into function

Hopefully a basic question, but is it all possible to typecast varibles when you define a function in JS? 希望是一个基本问题,但是当您在JS中定义函数时,是否有可能对变量进行类型转换? this would be useful when constructing a library for use by other developers. 当构建供其他开发人员使用的库时,这将很有用。

functionName:function(var1:string, var2:int)

instead of 代替

functionName:function(var1, var2)

No, this is not possible in normal Javascript. 不,这在常规Javascript中是不可能的。 Javascript is a completely dynamically-typed language. Javascript是一种完全动态类型的语言。 Types of variables are only known at runtime. 变量类型仅在运行时已知。 So all you can do is throw an exception when you get a wrong type. 因此,您所能做的就是在输入错误类型时引发异常。

There are, however, Javascript dialects like TypeScript or Dart which do have types and compile to normal Javascript. 但是,有些Java语言方言(例如TypeScript或Dart)确实具有类型并且可以编译为普通Javascript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM