简体   繁体   中英

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? 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 is a completely dynamically-typed language. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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