如何在javascript函数中定义数组参数。例如在c#中,我可以这样。
private void myFunc(params object[] values){
}
答案 0 :(得分:1)
JavaScript是动态类型的。任何参数都可以传递给数组。声明它没有特殊的语法。
function example(my_argument) {
if (my_argument instanceof Array) {
console.log(my_argument, "is an array");
} else {
console.log(my_argument, "is not an array");
}
}
example([1,2,3]);
example("a string");

答案 1 :(得分:0)
您没有在Javascript中指定参数类型,Javascript是动态类型语言。如果您想明确检查参数类型,那么您可以执行以下操作:
function(arg1) {
if (typeof arg1 !== 'object') {
throw new Error('Invalid type of argument');
}
}
另请查看this answer。