如何在函数内部分配函数调用返回的值[JS]

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

本文关键字:JS 返回 函数调用 函数 内部 分配      更新时间:2023-09-26

我的JavaScript代码中有两个函数,我想根据其他函数返回的值执行一些操作。

下面是我的代码:-

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
}

函数test2中的return是不正确的。那么我现在能做什么呢????


编辑

这是我想做的…但这似乎并不奏效http://jsfiddle.net/U6RjY/7/

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
}

去掉回车。

您将注意到,在函数test1中,它将在第一次循环时返回值。因此,它将停止执行。

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