如何在Javascript中的另一个函数中使用返回值? [英] How to use a return value in another function in Javascript?

查看:102
本文介绍了如何在Javascript中的另一个函数中使用返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自己自学JavaScript和出于好奇我想知道从一个函数返回一个值以在另一个函数中使用的正确方法是什么。例如:

I'm self-teaching myself JavaScript and out of curiosity I'm wondering what is the proper way of returning a value from one function to be used in another function. For example:

function firstFunction() {
  // do something;
  return somevalue 
}

那么如何设置第二个函数使用somevalue?谢谢。

So how do I set up the second function to use somevalue? Thanks.

推荐答案

调用该函数并保存该通话的返回值。

Call the function and save the return value of that very call.

function firstFunction() {
  // do something
  return "testing 123";
}

var test = firstFunction();  // this will grab you the return value from firstFunction();
alert(test);

您也可以从其他功能拨打此电话,只要两个功能具有相同的 < a href =https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions_and_function_scope\"rel =noreferrer>范围

You can make this call from another function too, as long as both functions have same scope.

例如:

function testCase() {
  var test = firstFunction(); 
  alert(test);
}

演示

Demo

这篇关于如何在Javascript中的另一个函数中使用返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆