如何将多个变量从函数传递给函数

编程入门 行业动态 更新时间:2024-10-28 04:20:21
本文介绍了如何将多个变量从函数传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这个问题是相关的,但与不同 javascript:在另一个函数中获取函数变量的值 & 如何在javascript中的函数之间传递变量

我一直想知道如何将变量从一个函数传递到另一个函数,可以找到答案以上。但更大的问题是如何传递多个变量,感觉上面找到的答案的语法会变得过于复杂,最终变得不可读。

I have been wondering for some time now how to pass a variable from one function to another, which answer can be found above. But the bigger question is how to pass multiple variable, sense the syntax of the answers found above can become overly complex, and it ends up being unreadable.

不要介意我在示例中添加和警告。 这是一个dummie示例,如下面的exaple所述。这个问题的目的在下面的段落中用粗体清楚地说明。

Don't mind the adding and alerting that I am doing in the example. This is a dummie example as stated below the exaple. The purpose of this question is clearly stated in the paragraph below in bold.

所以这是我的问题,你如何通过从一个函数到另一个函数的多个变量,例如:

So here is my question, How do you pass multiple variable from one function to another, example:

function definingVars() { var one = 1; var two = 2; var three = 3; var four = 4; var five = 5; var six = 6; var seven = 7; var eight = 8; var nine = 9; var ten = 10; } function addingUp() { definingVars(); alert("Alerting var total"); var total = one + two + three + four + five + six + seven + eight + nine; alert(total); alert("Alerting var ten"); alert(ten); }

我知道它不是正确的方法来解决这个问题但我想在这里做一个dummie示例。

I know that it is not the correct way to approach this. But I'm trying to make a dummie example here.

推荐答案

返回一个属性包含所需值的对象:

Return an object whose properties contain the desired values:

function definingVars() { return { one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7, eight: 8, nine: 9, ten: 10 }; } function addingUp() { var data = definingVars(); return data.one + data.two + data.three + data.four + data.five + data.six + data.seven + data.eight + data.nine; }

如果你想避免重复数据,你可以使用和,但请注意它是慢的,不好的做法,并且在严格模式下不允许:

If you want to avoid repeating data, you can use with, but note it's slow, bad practice and not allowed in strict mode:

function addingUp() { with(definingVars()) { return one + two + three + four + five + six + seven + eight + nine; } }

更多推荐

如何将多个变量从函数传递给函数

本文发布于:2023-11-23 18:13:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622416.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   多个   变量   如何将

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!