设置函数参数的types?

有没有办法让JavaScript函数知道某个参数是某种types?

能够做到这样的事情将是完美的:

function myFunction(Date myDate, String myString) { //do stuff } 

谢谢!

更新 :正如答案是一个响亮的“不”,如果我希望myDate被视为一个date(为了调用date函数),我不得不把它作为一个date内的函数或设置一个新的variablesDatetypes的呢?

不,JavaScript不是一个静态types的语言。 有时您可能需要手动检查函数体中的参数types。

不是在JavaScript自身,但使用谷歌闭合编译器的高级模式,你可以这样做:

 /** * @param {Date} myDate The date * @param {string} myString The string */ function myFunction(myDate, myString) { //do stuff } 

请参阅http://code.google.com/closure/compiler/docs/js-for-compiler.html

虽然您不能通知JavaScript有关types的语言 ,但您可以通知您的IDE有关它们,以便您获得更多有用的自动完成function。 要做到这一点,通过在/* comment */的参数之前指定types来使用types提示:

在这里输入图像说明

这是一个相当普遍的技术,例如ReactJS使用。 非常方便的传递给第三方库的callback参数。

不,相反,你需要根据你的需要做这样的事情:

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

查看来自Facebook的新stream程库,“一个静态types检查器,旨在查找JavaScript程序中的types错误”

定义:

 /* @flow */ function foo(x: string, y: number): string { return x.length * y; } foo('Hello', 42); 

types检查:

 $> flow hello.js:3:10,21: number This type is incompatible with hello.js:2:37,42: string 

这里是如何运行它 。

这不是内置的语言,但你可以很容易地做到这一点。 Vibhu的答案是我会考虑在Javascripttypes检查的典型方式。 如果你想要更广义的东西,可以尝试这样的事情:(只是一个例子,让你开始)

 typedFunction = function(paramsList, f){ //optionally, ensure that typedFunction is being called properly -- here's a start: if (!(paramsList instanceof Array)) throw Error('invalid argument: paramsList must be an array'); //the type-checked function return function(){ for(var i=0,p,arg;p=paramsList[i],arg=arguments[i],i<paramsList.length; i++){ if (typeof p === 'string'){ if (typeof arg !== p) throw new Error('expected type ' + p + ', got ' + typeof arg); } else { //function if (!(arg instanceof p)) throw new Error('expected type ' + String(p).replace(/\s*\{.*/, '') + ', got ' + typeof arg); } } //type checking passed; call the function itself return f.apply(this, arguments); } } //usage: var ds = typedFunction([Date, 'string'], function(d, s){ console.log(d.toDateString(), s.substr(0)); }); ds('notadate', 'test'); //Error: expected type function Date(), got string ds(); //Error: expected type function Date(), got undefined ds(new Date(), 42); //Error: expected type string, got number ds(new Date(), 'success'); //Fri Jun 14 2013 success 

ArgueJS可以轻易做到这一点 :

 function myFunction () { arguments = __({myDate: Date, myString: String}); // do stuff }; 

ES6的到来,为新的演员打开了舞台。

 function myFunction(_d, _s, myDate=new Date(_d), myString=String(_s) ){ console.log("MyDate (",typeof myDate ,") => ", myDate); console.log("MyString (", typeof myString,") => ", myString); } myFunction("October 13, 2014 11:13:00", 123); // CONSOLE OUTPUT: // MyDate ( object ) => Mon Oct 13 2014 11:13:00 GMT+0200 (Hora de verano romance) // MyString ( string ) => 123 

在function上不是一个精彩的实现types参数,而是一个概念validation。在Chrome 59.0.3071.115中testing。 推荐讲座: ES6参数处理

使用typeofinstanceof

 const assert = require('assert'); function myFunction(Date myDate, String myString) { assert( typeof(myString) === 'string', 'Error message about incorrect arg type'); assert( myDate instanceof Date, 'Error message about incorrect arg type'); }