gpt4 book ai didi

javascript - 获取被调用者 Function.Arguments 之一的 Function.Arguments

转载 作者:行者123 更新时间:2023-12-02 16:39:01 27 4
gpt4 key购买 nike

function validateArguments(args)
{
if(args.length < 1 && args.length > 2)
{
throw new RangeError("Invalid amount of arguments. Follow the syntax");
}
if(typeof args[0] !== "object")
{
throw new TypeError("Invalid first argument type. Follow the syntax");
}
if(args[1] && typeof args[1] !== "function")
{
throw new TypeError("Invalid second argument type. Follow the syntax");
}
return true;
}

我想弄清楚的是对于args[1],如果它是一个函数,也可以获取它的参数列表。这可能吗?基本上,这是一个示例代码。

someFunction({ object:"//object" },function(data,datas,dataCall){
//is data, datas, and dataCall too many arugments.
//if so how to throw the error here
});

最佳答案

你的args只是第一个参数。有一个特殊的arguments捕获传递给函数的所有给定参数的变量。您可以像这样使用它:

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
}

我不确定你在问什么。如果您想在其他函数内部使用 validateArguments 函数来检查它们的参数,您可以通过将它们的 arguments 对象/数组(实际上类似于数组)传递给检查员:

function someFunction () {
validateArguments(arguments)

// do stuff
}

我不明白你最后的回调位是什么意思。如果你想限制参数,回调函数将会让你不走运。您不能对函数声明施加限制(如果您不是自己编写它们),但您可以控制传递给它们的内容,因此...

关于javascript - 获取被调用者 Function.Arguments 之一的 Function.Arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27671871/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com