解析字符串中的布尔值与Function.apply使用字符串、布尔值、apply、Function

2023-09-08 15:33:01 作者:一世情人,ァ

我用 String.split 来解析命令行字符串转换成字符串数组。然后将结果用于调用使用 Function.apply API函数。

I'm using String.split to parse a command line string into an array of strings. The result is then used to call a function using the Function.apply API.

如果申请(NULL,[17])被调用这个函数:

static function test(foo:int):void
{
    trace(foo, typeof(foo));
}

它按预期工作(输出: 17号)。

但是,调用申请(NULL,[假])申请(NULL,[0])具有这种功能:

However, calling apply(null, ["false"]) or apply(null, ["0"]) with this function:

static function test(foo:Boolean):void
{
    trace(foo, typeof(foo));
}

不工作(预期输出:假布尔;实际输出:真布尔)。

does not work (expected output: false Boolean; actual output: true Boolean).

有没有一种方法,使之认识到真实(或其他任何东西)作为布尔值,就像它与数字串?理想情况下真实也应该保持有效的字符串值。

Is there a way to make it recognize "true" and "false" (or anything else) as Boolean values, just like it does with numerical strings? Ideally "true" and "false" should also remain valid string values.

推荐答案

没有,没有国米preT字符串内置的方式真与假的布尔值。

No, there is no built in way to interpret the strings "true" and "false" as booleans.

在ActionScript和其他ECMA脚本语言中,被评估为布尔值false唯一的字符串值是一个空字符串。任何字符串的长度被评价为真正的布尔EX pression,包括字符串假和0。

In ActionScript, and other ECMA script languages, the only string value that gets evaluated as boolean false is an empty string, "". Any string with a length gets evaluated as true in a boolean expression, including the strings "false" and "0".

在实践中,这不是一个大问题,因为它是很容易做一个字符串比较。一堆在这里建议:http://stackoverflow.com/questions/263965/how-can-i-convert-a-string-to-boolean-in-javascript

In practice, this is seldom a big problem, since it is quite easy to make a string comparison. A bunch of suggestions here: http://stackoverflow.com/questions/263965/how-can-i-convert-a-string-to-boolean-in-javascript