Javascript 获取其中一个被调用函数的Function.Arguments.Arguments
你可以这样做Javascript 获取其中一个被调用函数的Function.Arguments.Arguments,javascript,arguments,throw,Javascript,Arguments,Throw,你可以这样做 someFunction({ object:"//object" },function(data,datas,dataCall){ //is data, datas, and dataCall too many arugments. //if so how to throw the error here }); 您可以使用参数,它将包含所有参数列表。您的args只是第一个参数。有一个特殊的变量可以捕获传递给函数的所有给定参数。您可以这样使用它: someFunction
someFunction({ object:"//object" },function(data,datas,dataCall){
//is data, datas, and dataCall too many arugments.
//if so how to throw the error here
});
您可以使用参数,它将包含所有参数列表。您的
args
只是第一个参数。有一个特殊的变量可以捕获传递给函数的所有给定参数。您可以这样使用它:
someFunction: function() {
if(validateArguments(arguments)) {
var object = arguments.shift();
var func = arguments.shift();
func.apply(this,arguments);
// Do whatever you want
}
},
我不明白你所说的最后回叫是什么意思。如果你想限制参数,回调函数会让你运气不好。您不能对函数声明施加限制(如果您不是自己编写的),但您可以控制传递给它们的内容,因此
我试图弄清楚的是,如果它是一个函数,那么就为args[1]
获取该函数的参数列表。这可能吗
有点,但不是100%可靠。您可以访问函数的属性,该属性返回函数的arity。例如:
function someFunction () {
validateArguments(arguments)
// do stuff
}
如果您想获得参数的名称,可以将函数转换为字符串并提取参数列表,如所示
但是,由于函数即使没有正式定义的参数也可以访问其参数(通过
参数
),因此这不是一种可靠的技术。但是这两种方法都没有。args是要传递的参数,在另一个函数中被调用,就像这样isValidArguments(参数)
我将查看每个人,看看我能做些什么
function myFunction () {
if (arguments.length < 1 || arguments.length > 2)
throw new RangeError("Invalid amount of arguments. Follow the syntax")
if (typeof arguments[0] !== "object")
throw new TypeError("Invalid first argument type. Follow the syntax")
if (typeof arguments[1] !== "function")
throw new TypeError("Invalid second argument type. Follow the syntax")
// do stuff
}
function someFunction () {
validateArguments(arguments)
// do stuff
}
function foo(a, b, c) {
}
foo.length;
// 3