Set type for function parameters?

后端 未结 12 1843
醉话见心
醉话见心 2020-11-30 18:37

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:



        
12条回答
  •  时光说笑
    2020-11-30 19:02

    No, instead you would need to do something like this depending on your needs:

    function myFunction(myDate, myString) {
      if(arguments.length > 1 && typeof(Date.parse(myDate)) == "number" && typeof(myString) == "string") {
        //Code here
      }
    }
    

提交回复
热议问题