Javascript 如何在javascript中返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5887386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:22:31  来源:igfitidea点击:

How to return values in javascript

javascript

提问by user695663

I have a javascript function:

我有一个 javascript 函数:

function myFunction(value1,value2,value3)
{
     //Do stuff and 

     value2=somevalue2 //to return
     value3=somevalue3 //to return
}

function call in Code: ....

代码中的函数调用:....

myFunction("1",value2,value3);

if(value2 && value3)
{
//Do some stuff
}

in this scenario how to pass value2 and value3 to the called method or how to return values in Java script.

在这种情况下如何将 value2 和 value3 传递给被调用的方法或如何在 Java 脚本中返回值。

回答by McStretch

You can return an array, an object literal, or an object of a type you created that encapsulates the returned values.

您可以返回数组、对象字面量或您创建的封装返回值的类型的对象。

Then you can pass in the array, object literal, or custom object into a method to disseminate the values.

然后,您可以将数组、对象字面量或自定义对象传递到方法中以传播值。

Object example:

对象示例:

function myFunction(value1,value2,value3)
{
     var returnedObject = {};
     returnedObject["value1"] = value1;
     returnedObject["value2"] = value2;
     return returnedObject;
}

var returnValue = myFunction("1",value2,value3);

if(returnValue.value1  && returnValue.value2)
{
//Do some stuff
}

Array example:

数组示例:

function myFunction(value1,value2,value3)
{
     var returnedArray = [];
     returnedArray.push(value1);
     returnedArray.push(value2);
     return returnedArray;
}

var returnValue = myFunction("1",value2,value3);

if(returnValue[0]  && returnValue[1])
{
//Do some stuff
}

Custom Object:

自定义对象:

function myFunction(value1,value2,value3)
{
     var valueHolder = new ValueHolder(value1, value2);
     return valueHolder;
}

var returnValue = myFunction("1",value2,value3);

// hypothetical method that you could build to create an easier to read conditional 
// (might not apply to your situation)
if(returnValue.valid())
{
//Do some stuff
}

I would avoid the array method because you would have to access the values via indices rather than named object properties.

我会避免使用数组方法,因为您必须通过索引而不是命名对象属性来访问值。

回答by Chris Kooken

Javascript is duck typed, so you can create a small structure.

Javascript 是鸭子类型的,因此您可以创建一个小结构。

function myFunction(value1,value2,value3)
{         
     var myObject = new Object();
     myObject.value2 = somevalue2;
     myObject.value3 = somevalue3;
     return myObject;
}


var value = myFunction("1",value2,value3);

if(value.value2  && value.value3)
{
//Do some stuff
}

回答by rob

function myFunction(value1,value2,value3)
{         
     return {val2: value2, val3: value3};
}

回答by ezmilhouse

I would prefer a callback solution: Working fiddle here: http://jsfiddle.net/canCu/

我更喜欢回调解决方案:在这里工作小提琴:http: //jsfiddle.net/canCu/

function myFunction(value1,value2,value3, callback) {

    value2 = 'somevalue2'; //to return
    value3 = 'somevalue3'; //to return

    callback( value2, value3 );

}

var value1 = 1;
var value2 = 2;
var value3 = 3;

myFunction(value1,value2,value3, function(value2, value3){
    if (value2 && value3) {
        //Do some stuff
        alert( value2 + '-' + value3 );
    }    
});

回答by no.good.at.coding

It's difficult to tell what you're actually trying to do and if this is what you really need but you might also use a callback:

很难说出你真正想要做什么,以及这是否是你真正需要的,但你也可以使用回调:

function myFunction(value1,callback)
{
     //Do stuff and 

     if(typeof callback == 'function'){
        callback(somevalue2,somevalue3);
    }
}


myFunction("1", function(value2, value3){
    if(value2 && value3)
    {
    //Do some stuff
    }
});

回答by Joomler

The return statementstops the execution of a function and returns a valuefrom that function.

return statement停止功能和执行returns a value从功能。

While updating global variables is one way to pass information back to the code that called the function, this is not an ideal way of doing so. A much better alternative is to write the function so that values that are used by the function are passed to it as parameters and the function returns whatever value that it needs to without using or updating any global variables.

虽然更新全局变量是将信息传递回调用 的代码的一种方式function,但这并不是一种理想的方式。一个更好的选择是编写函数,以便函数使用的值作为参数传递给它,函数返回它需要的任何值,而不使用或更新任何global variables.

By limiting the way in which information is passed to and from functions we can make it easier to reuse the same function from multiple places in our code.

通过限制与函数之间传递信息的方式,我们可以更轻松地在代码中的多个位置重用相同的函数。

JavaScript provides for passing one value back to the code that called it after everything in the function that needs to run has finished running.

JavaScript 允许在需要运行的函数中的所有内容都完成运行后将一个值传递回调用它的代码。

JavaScript passes a value from a function back to the code that called it by using the return statement. The value to be returned is specified in the returnkeyword.

JavaScript 使用return statement. 要返回的值在return关键字中指定。

回答by pgreen2

The answers cover things very well. I just wanted to point out that the mechanism of out parameters, as described in the question isn't very javascriptish. While other languages support it, javascript prefers you to simply return values from functions.

答案很好地涵盖了事情。我只是想指出,out parameters问题中描述的 ,机制不是很javascriptish。虽然其他语言支持它,但 javascript 更喜欢您简单地从函数返回值。

With ES6/ES2015 they added destructuringthat makes a solution to this problem more elegant when returning an array. Destructuring will pull parts out of an array/object:

在 ES6/ES2015 中,他们添加了解构,这使得在返回数组时更优雅地解决这个问题。解构将从数组/对象中提取部分:

function myFunction(value1)
{
    //Do stuff and 
    return [somevalue2, sumevalue3]
}

var [value2, value3] = myFunction("1");

if(value2  && value3)
{
    //Do some stuff
}

回答by raj peer

Its very simple. Call one function inside another function with parameters.

它非常简单。使用参数在另一个函数中调用一个函数。

    function fun1()
    {
     var a=10;
     var b=20;
     fun2(a,b); //calling function fun2() and passing 2 parameters
    }

   function fun2(num1,num2)
   {
    var sum;
    sum = num1+num2;
    return sum;
   }

   fun1(); //trigger function fun1