0

我有以下问题:

我有一个接收多个变量的函数:

function test( fooValue:String, foobar1:String, foobar2:String, goobar1:String, goobar2:String ) {
//using the values statically
mytext1.text = foobar1;
mytext2.text = foobar2;
mytext3.text = goobar1;
mytext4.text = goobar2;

if ( goobar1 = "problem" ) {
    myProblem.text = this["foo" + fooValue] + this["goo" + fooValue];
}
}
//now here's an example call
test( "bar1","first value ","second value ", "another value", "yet another value");

鉴于 fooValue 在上述调用中具有“bar1”这一事实,我怎样才能使 myProblem.text 显示“第一个值另一个值”

this[ "foo" + fooValue] 给我 undefined

  • 编辑了问题并试图更具体。
4

1 回答 1

1

简单的。

function test( fooValue:String, foobar1:String, foobar2:String, goobar1:String, goobar2:String ) {
//using the values statically
mytext1.text = foobar1;
mytext2.text = foobar2;
mytext3.text = goobar1;
mytext4.text = goobar2;

if ( goobar1 == "problem" ) {
    myProblem.text = this["foo" + fooValue] + this["goo" + fooValue];
}
}
//now here's an example call
test( "bar1","first value ","second value ", "another value", "yet another value")

注意到变化了吗?它是额外=if(goobar1...

goobar1 = "problem"设置 goobar1 的值。

goobar1 == "problem"返回 goobar1 的值是否为“问题”

菜鸟的错误,有时也是有经验的人犯的:)

此外

this["foo" + fooValue]等效于this.foobar1(这是无效的,因为该this对象没有任何名为 的属性foobar1

你这样做:

switch(fooValue) {
    case "bar1":
        myProblem.text = foobar1 + goobar1;
        break;
    case "bar2":
        myProblem.text = foobar2 + goobar2;
        break;
}
于 2012-07-26T13:51:50.810 回答