什么是函数值函数类型的flex(ActionScript3)语法?

时间:2023-01-09 22:29:29

What is the syntax to declare a type for my compare-function generator in code like the following?

在如下代码中为比较函数生成器声明类型的语法是什么?

var colName:String = ""; // actually assigned in a loop
gc.sortCompareFunction = function() : ??WHAT_GOES_HERE??
{
   var tmp:String = colName;
   return function(a:Object,b:Object):int { return compareGeneral(a,b,tmp); };
}();

2 个解决方案

#1


1  

Isn't "Function" a data type?

“功能”不是数据类型吗?

#2


0  

In order to understand what the data type is, we must know what the intended outcome of the return is. I need to see the code block for compareGeneral, and I still don't believe this will help. You have two returns withing the same function "gc.sortCompareFunction", I believe this is incorrect as return gets a value and then acts as a break command meaning the rest of the anything withing the same function block is ignored. The problem is that I don't know which return is the intended return, and I don't know that flash knows either. You can use * as a data type, but this should only really be used in specific situations. In this situation I believe you need only the one return value that merely returns whatever the value of compareGeneral.

为了理解数据类型是什么,我们必须知道返回的预期结果是什么。我需要看看compareGeneral的代码块,我仍然不相信这会有所帮助。你有两个具有相同功能“gc.sortCompareFunction”的返回,我相信这是不正确的,因为return得到一个值然后充当break命令意味着忽略具有相同功能块的其余任何东西。问题是我不知道哪个返回是预期的返回,我不知道flash也知道。您可以使用*作为数据类型,但这应该只在特定情况下使用。在这种情况下,我相信你只需要一个只返回compareGeneral值的返回值。

Now if this is a compareGenerator it really should either return a Boolean TRUE or FALSE, or a int 0 or 1, lets use the former. Also I believe we can use one less function. Since I have not seen all of your code and I am not exactly sure what your trying to accomplish, the following is hypothetical.

现在,如果这是一个compareGenerator,它应该返回一个布尔值TRUE或FALSE,或者一个int 0或1,让我们使用前者。另外我相信我们可以少用一个功能。由于我没有看到你的所有代码,而且我不确定你想要完成什么,以下是假设的。

function compareGeneral(a:object,b:object):Boolean
{
   //Check some property associated to each object for likeness.
   if(a.someAssignedPropery == b.someAssignedPropery)
   {
      return true;
   }
   return false;
}
var objA:Object = new Object();
objA.someAssignedProperty = "AS3";
objB.someAssignedProperty = "AS3";

compareGeneral(objA,objB);

In this case compareGeneral(objA,objB); returns true, though we haven't done anything useful with it yet. Here is a way you may use it. Remember that it either returns a value of true or false so we can treat it like a variable.

在这种情况下,compareGeneral(objA,objB);返回true,虽然我们还没有做任何有用的事情。这是您可以使用它的方式。请记住,它返回true或false值,因此我们可以将其视为变量。

if(compareGeneral(objA,objB)) //same as if(compareGeneral(objA,objB)) == true)
{
   trace("You have found a match!");
   //Here you can call some other function or set a variable or whatever you require functionality wise based on a match being found.
}
else
{
  trace("No match could be found!");
}

I hope that this is able to help you understand data types and return values. I do not know what you were doing with tmp, but generally functions that return a value deal with that one thing and only that thing, so it is best that the compare function compare one thing against the other and that be the extent of the call. Whatever functionality you require with tmp can go inside its own function or method, and be called when needed.

我希望这能够帮助您理解数据类型和返回值。我不知道你在使用tmp做了什么,但通常返回一个值的函数处理那个东西只有那个东西,所以最好比较函数比较一个东西和另一个东西,那就是调用的程度。无论tmp需要什么功能,都可以进入自己的功能或方法,并在需要时调用。

#1


1  

Isn't "Function" a data type?

“功能”不是数据类型吗?

#2


0  

In order to understand what the data type is, we must know what the intended outcome of the return is. I need to see the code block for compareGeneral, and I still don't believe this will help. You have two returns withing the same function "gc.sortCompareFunction", I believe this is incorrect as return gets a value and then acts as a break command meaning the rest of the anything withing the same function block is ignored. The problem is that I don't know which return is the intended return, and I don't know that flash knows either. You can use * as a data type, but this should only really be used in specific situations. In this situation I believe you need only the one return value that merely returns whatever the value of compareGeneral.

为了理解数据类型是什么,我们必须知道返回的预期结果是什么。我需要看看compareGeneral的代码块,我仍然不相信这会有所帮助。你有两个具有相同功能“gc.sortCompareFunction”的返回,我相信这是不正确的,因为return得到一个值然后充当break命令意味着忽略具有相同功能块的其余任何东西。问题是我不知道哪个返回是预期的返回,我不知道flash也知道。您可以使用*作为数据类型,但这应该只在特定情况下使用。在这种情况下,我相信你只需要一个只返回compareGeneral值的返回值。

Now if this is a compareGenerator it really should either return a Boolean TRUE or FALSE, or a int 0 or 1, lets use the former. Also I believe we can use one less function. Since I have not seen all of your code and I am not exactly sure what your trying to accomplish, the following is hypothetical.

现在,如果这是一个compareGenerator,它应该返回一个布尔值TRUE或FALSE,或者一个int 0或1,让我们使用前者。另外我相信我们可以少用一个功能。由于我没有看到你的所有代码,而且我不确定你想要完成什么,以下是假设的。

function compareGeneral(a:object,b:object):Boolean
{
   //Check some property associated to each object for likeness.
   if(a.someAssignedPropery == b.someAssignedPropery)
   {
      return true;
   }
   return false;
}
var objA:Object = new Object();
objA.someAssignedProperty = "AS3";
objB.someAssignedProperty = "AS3";

compareGeneral(objA,objB);

In this case compareGeneral(objA,objB); returns true, though we haven't done anything useful with it yet. Here is a way you may use it. Remember that it either returns a value of true or false so we can treat it like a variable.

在这种情况下,compareGeneral(objA,objB);返回true,虽然我们还没有做任何有用的事情。这是您可以使用它的方式。请记住,它返回true或false值,因此我们可以将其视为变量。

if(compareGeneral(objA,objB)) //same as if(compareGeneral(objA,objB)) == true)
{
   trace("You have found a match!");
   //Here you can call some other function or set a variable or whatever you require functionality wise based on a match being found.
}
else
{
  trace("No match could be found!");
}

I hope that this is able to help you understand data types and return values. I do not know what you were doing with tmp, but generally functions that return a value deal with that one thing and only that thing, so it is best that the compare function compare one thing against the other and that be the extent of the call. Whatever functionality you require with tmp can go inside its own function or method, and be called when needed.

我希望这能够帮助您理解数据类型和返回值。我不知道你在使用tmp做了什么,但通常返回一个值的函数处理那个东西只有那个东西,所以最好比较函数比较一个东西和另一个东西,那就是调用的程度。无论tmp需要什么功能,都可以进入自己的功能或方法,并在需要时调用。