设置函数参数的类型? [英] Set type for function parameters?
本文介绍了设置函数参数的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法让JavaScript函数知道某个参数是某种类型的?
能够做到这样的事情是完美的:
函数myFunction(Date myDate,String myString)
{
// do stuff
}
谢谢!
更新:如果我想将 myDate
作为日期(为了调用它的日期函数),答案是响亮的否 ,我必须将它作为日期内的日期转换或设置Date类型的新变量吗?
解决方案
不, JavaScript不是一种静态类型的语言。有时您可能需要手动检查函数体中的参数类型。
Is there a way to let a javascript function know that a certain parameter is of a certain type?
Being able to do something like this would be perfect:
function myFunction(Date myDate, String myString)
{
//do stuff
}
Thank you!
Update: Being that the answer is a resounding "no," if I want myDate
to be treated as a date (in order to call date functions on it), I have to cast it as a date inside the function or set a new variable of type Date to it?
解决方案
No, JavaScript is not a statically typed language. Sometimes you may need to manually check types of parameters in your function body.
这篇关于设置函数参数的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文