下面是一个带有命名参数的常规函数:
function who(name, age, isMale, weight)
{
alert(name + ' (' + (isMale ? 'male' : 'female') + '), ' + age + ' years old, ' + weight + ' kg.');
}
who('Jack', 30, true, 90); //this is OK.
Run Code Online (Sandbox Code Playgroud)
我想要的是; 是否按顺序传递参数; 该函数应该产生类似的结果(如果不相同):
who('Jack', 30, true, 90); //should produce the same result with the regular function
who(30, 90, true, 'Jack'); //should produce the same result
who(true, 30, 'Jack', 90); //should produce the same result
Run Code Online (Sandbox Code Playgroud)
这使您可以按任何顺序传递参数列表,但仍将映射到逻辑顺序.我到目前为止的做法是这样的:
function who()
{ …Run Code Online (Sandbox Code Playgroud) 这是简化的 JSON-Schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "user",
"type": "object",
"properties": {
"account": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["COMPANY", "PERSON"]
}
},
"required": ["type"]
},
"person": {
"type": "object",
"properties": {
"firstName": { "type": "string" },
"lastName": { "type": "string" }
},
"required": ["firstName", "lastName"]
},
"company": {
"type": "object",
"properties": {
"name": { "type": "string" },
"taxNumber": { "type": "string" }
}
}
},
"required": ["account", "person"]
}
Run Code Online (Sandbox Code Playgroud)
我想要实现的是:
account.type设置为"COMPANY":
company …我测试了以下代码:
function aa(...aArgs):void
{
trace("aa:", aArgs.length);
bb(aArgs);
}
function bb(...bArgs):void
{
trace("bb:", bArgs.length);
}
aa(); //calling aa without any arguments.
Run Code Online (Sandbox Code Playgroud)
输出是:
aa: 0 //this is expected.
bb: 1 //this is not!
Run Code Online (Sandbox Code Playgroud)
当我将空参数(aArgs)传递给bb函数时; 它不应该返回0长度?好像函数bb将传递的aArgs视为非空/非null.
我在这里错过了什么?
任何帮助表示赞赏.问候..
为什么你认为下面的代码不起作用?你会改变/添加什么使它工作?
任何帮助表示赞赏..
function TraceIt(message:String, num:int)
{
trace(message, num);
}
function aa(f:Function, ...args):void
{
bb(f, args);
}
aa(TraceIt, "test", 1);
var func:Function = null;
var argum:Array = null;
function bb(f:Function, ...args):void
{
func = f;
argum = args;
exec();
}
function exec()
{
func.apply(null, argum);
}
Run Code Online (Sandbox Code Playgroud)
我得到一个ArgumentError(错误#1063):
Argument count mismatch on test_fla::MainTimeline/TraceIt(). Expected 2, got 1.
Run Code Online (Sandbox Code Playgroud)
..因此,传递的参数(argum)无法提供所有传递的参数.
..请保持功能结构(流量)完整.我需要一个使用相同函数的解决方案.我必须将args传递给变量并在上面的exec()方法中使用它们.
问候
arguments ×3
function ×3
dependencies ×1
javascript ×1
jquery ×1
json ×1
jsonschema ×1
parameters ×1
rest ×1