简体   繁体   English

如何在另一个函数[JS]中分配函数调用返回的值

[英]How to assign value returned by a function call inside another function [JS]

I am having two functions in my JavaScript code, and I want to perform some action on the basis of value returned by other function. 我的JavaScript代码中有两个函数,我想根据其他函数返回的值执行一些操作。

Here my code :- 我的代码在这里: -

function test1(){
    var radio = document.getElementByName('sample');
    for (i=0; i<radio.length; i++){
         //some code here
         return "some value on basis of above code"
    }
}

function test2(){
    var somevariable = globllysetValue;
    var returnValue = return test1();
    // some code and work according to the value in returnValue
}

Well I know return in function test2 is not correct. 我知道函数test2 return不正确。 so what can I do here NOW???? 那我现在该怎么办?


Edit 编辑

Here what I am Trying to do... but it do not seem to be working http://jsfiddle.net/U6RjY/7/ 这就是我想要做的......但它似乎没有起作用http://jsfiddle.net/U6RjY/7/

EDIT2 ----- The corrected and working fiddle... http://jsfiddle.net/U6RjY/9/ Thanks to all :) EDIT2 -----纠正和工作的小提琴... http://jsfiddle.net/U6RjY/9/感谢所有:)

function test1(){
    var radio = document.getElementByName('sample');
     for (i=0; i<radio.length; i++){
         //some code here
         return "some value on basis of above code"
    }
}

function test2(){
    var somevariable = globllysetValue;
    var returnValue = test1();
    // some code and work according to the value in returnValue
}

Just remove the return. 只需删除退货。

You will notice however that in function test1, it will return the value on the first loop. 但是你会注意到,在函数test1中,它将返回第一个循环的值。 So, it will stop executing. 所以,它将停止执行。

Live example: http://jsfiddle.net/U6RjY/ 实例: http//jsfiddle.net/U6RjY/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM