通过实际的变量对象给一个函数变量、实际、对象、一个函数

2023-09-08 15:09:35 作者:薄荷微光少年

这个问题很简单: 我需要实际的变量传递给函数。

The problem is simple: I need to pass actual variable to a function.

private var test:String = "KKK";
trace (" Before --->>> " + test);
testFunction(test);
trace (" Next --->>> " + test);

private function testFunction(d:String):void{
   d = "MMM";
}

结果:

Before --->>> KKK
Next --->>> KKK

的结果是正确的,但,我要的是,发送实际测试变量我的功能和更改什么。所以,我想有这样的输出:

The result is correct but, What I want is, send the actual test variable to my function and change that. So I want to have output like this:

Before --->>> KKK
Next --->>> MMM

任何解决方案?

Any solution?

谢谢您的回答,但如果我有一个code这样的,我需要的实际变量传递给我的功能:

Thanks for your answer but if I have a code like this, I need to pass the actual variable to my function:

if ( lastPos == -1 ){// if this is first item 
    flagLEFT = "mid";
    tempImageLEFT = new Bitmap(Bitmap(dataBANK[0]["lineimage" + 10]).bitmapData);
}else if (nextPos == -1){// if this is the last position
    flagRIGHT = "mid";
    tempImageRGHT = new Bitmap(Bitmap(dataBANK[0]["lineimage" + 13]).bitmapData);
}

正如你看到的,变化是 flagLEFT tempImageRGHT 。另外,我对数字(10和13)的变化,可以在正常的方式处理。我需要的是这样的:

As you see, changes are in flagLEFT and tempImageRGHT . Also I have a change on numbers (10 and 13) which can be handle in normal way. I need something like this:

private function itemFirstLast(flag:String, bmp:Bitmap, pos:int):void{
    flag = "mid";
    bmp = new Bitmap(Bitmap(dataBANK[0]["lineimage" + pos]).bitmapData);
}

任何解决方案?

Any solution?

推荐答案

一种方法是返回新的字符串,并将其分配给测试:

One way is to return the new string and assign it to test :

private var test:String = "KKK";
trace (" Before --->>> " + test);
test = testFunction(test);
trace (" Next --->>> " + test);

private function testFunction(d:String):String{
   d = "MMM";
   return d;
}

这仍然没有通过实际的字符串对象,但测试字符串会发生变化。字符串是按值在AS3过去了,如果你wan't真正通过它,你可以在一个对象包装它:

This still doesn't pass the actual string object but the test string will change. Strings are passed by value in AS3, if you wan't to actually pass it in you can wrap it in an object :

var object:Object {
   "test":"KKK"
};
trace (" Before --->>> " + object["test"]);
testFunction(object);
trace (" Next --->>> " + object["test"]);

private function testFunction(o:Object):void{
    o["test"] = "MMM";
}
 
精彩推荐