gpt4 book ai didi

javascript - 从函数返回多个值

转载 作者:行者123 更新时间:2023-11-28 15:31:06 29 4
gpt4 key购买 nike

我有一个 js 文件(如下),其中每个函数返回 2 个结果(Step1 和 Step2)。我需要将这些结果转换成...

“将”+(第 1 步产品类型)+“与“+(第 1 步过氧化物)+“混合。”+(第 1 步计时)

使用“+(Step2产品类型)+”,将“+(Step2toneCalc)+”与“+(Step2过氧化物)+”混合。 "+ (Step2 计时)

将这些结果转换为上述格式的最佳方法是什么?
我知道如何处理单个值,但不太确定如何处理包含在一个函数中的多个函数。

谢谢

function doubleprocess(type) {

function productType() {
var step1;
var step2;
if (type == "light") {
step1 = "lightner";
step2 = "demi-color";
} else {
step1 = "demi-color";
step2 = "demi-color";
}
return step1, step2;
}


function toneCalc() {
var step1;
var step2;

if (type == "light") {
step1 = "light gold";
} else if (type == "dark") {
step1 = "80% gold with 20% red"
} else {
step1 = "another tone";
}

step2 = "neutral";

return step1, step2;
}

function peroxide() {
var step1;
var step2;

if (type == "light") {
step1 == "20V/6% peroxide";
} else if (type == "dark") {
step1 = "something else";
} else {
step1 = "Do something.";
}
step2 = "Do something else.";
return step1, step2;
}

function level() {
var step1 = "";
var step2;

if (type == "dark") {
step1 = "Do something.";
} else {
step1 = "Do Nothing.";
};

step2 = "Do something.";

return step1, step2;
}

function timing() {
var step1;
var step2;
if (type == "light") {
step1 == "Do something.";
step2 = "Do something.";
} else if (type == "dark") {
step1 = "Do something.";
step2 = "Do something.";
} else {
step1 = "Do something.";
step2 = "Do something.";
}
return step1, step2;
}
}

最佳答案

不能从一个函数返回多个变量。但是您可以返回一个对象。

function peroxide() {
var step1;
var step2;

if (type == "light") {
step1 == "20V/6% peroxide";
} else if (type == "dark") {
step1 = "something else";
} else {
step1 = "Do something.";
}
step2 = "Do something else.";
var rtn = new Object();
rtn.step1 = step1;
rtn.step2 = step2;
return rtn;
}

关于javascript - 从函数返回多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27326897/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com