在函数之间传递值

发布于 2024-07-25 19:01:32 字数 91 浏览 8 评论 0原文

大家好,有什么方法可以在函数中本地定义变量,然后将其传递给其他函数。 我的意思是说是否有可能将本地值从一个函数传递到另一个函数。 有人请建议我解决方案。 提前致谢

Hi All is there any way to locally define a variable in a function and then pass it to the oher function. I mean to say is it possible the pass a local value from one function to other function.
Somebody Please suggest me the solution.
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨落成白 2024-08-01 19:01:32

或者就是这么简单或者你的意思是别的:

private function function1():void
{
    var localVariable:String = "this is local variable of function1()";
    function2(localVariable);
}

private function function2(string:String):void
{
    trace(string);
}

function1();

或者使用全局变量作为临时存储:

private var globalVariable:String = "";

private function function1():void
{
    var localVariable:String = "this is local variable of function1()";

    globalVariable = localVariable;
}

private function function2():void
{
    trace(globalVariable);
}

function1();
function2();

Or it's that simple or you meant something else:

private function function1():void
{
    var localVariable:String = "this is local variable of function1()";
    function2(localVariable);
}

private function function2(string:String):void
{
    trace(string);
}

function1();

or use global variable as temporary storage:

private var globalVariable:String = "";

private function function1():void
{
    var localVariable:String = "this is local variable of function1()";

    globalVariable = localVariable;
}

private function function2():void
{
    trace(globalVariable);
}

function1();
function2();
穿越时光隧道 2024-08-01 19:01:32

兹德米特里夫是对的。

虽然,您也可以创建默认变量,如下所示:(

修改 zdmytriv 的代码)

private function function1():void
{
    var localVariable:String = "this is local variable of function1()";
    function2(localVariable);
    function2(); //You don't have to enter a default argument
}

private function function2(string:String = "something else"):void
{
    trace(string);
}

这将跟踪:

this is local variable of function1()
something else

有点偏离主题,但很高兴知道。

zdmytriv is right.

Although, you can also make default variables, like so:

(Modifying zdmytriv's code)

private function function1():void
{
    var localVariable:String = "this is local variable of function1()";
    function2(localVariable);
    function2(); //You don't have to enter a default argument
}

private function function2(string:String = "something else"):void
{
    trace(string);
}

This would trace:

this is local variable of function1()
something else

A little off topic, but good to know.

盗梦空间 2024-08-01 19:01:32

Flex 中的基元按值传递,而复杂对象则按引用传递。 您可以使用它来传递对象,而无需将变量的范围限定在函数本身之外。 例如:

private function function1():void {
{
     var localVar:Object = {value:"test"};
     trace(localVar.value);
     function2(localVar);
     trace(localVar.value);
}

private function function2(obj:Object):void
{
     obj.value = "new value";
}

这将跟踪:

test
new value

这反映了 function2 通过引用接收参数“obj”这一事实,作为指向原始“localVar”对象的指针。 当它设置 .value 字段时,该更改会反映在 function1 中。

我只是想指出这一点。

Primitives in Flex are passed by value, where complex objects are passed by reference. You can use this to pass objects around without scoping a variable outside the functions themselves. For instance:

private function function1():void {
{
     var localVar:Object = {value:"test"};
     trace(localVar.value);
     function2(localVar);
     trace(localVar.value);
}

private function function2(obj:Object):void
{
     obj.value = "new value";
}

This would trace:

test
new value

Which reflects the fact that function2 receives the parameter "obj" by reference, as a pointer to the original "localVar" object. When it sets the .value field, that change is reflected in function1.

I just thought I'd point that out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文