0

我最近进入了一个项目,该项目需要两种不同的方法来处理两个不同的变量,但我需要存储这两个值以便在另一个函数中使用它们。

这有点复杂,所以让我解释一下。我有这个 switch 语句可以监视几个数字:2、3、13、45

var hello;
switch ($variable) {
    case 2:
        //do something irrelevant
        break;
    case 3:
        //do something irrelevant
        break;
    case 13:
        //do something with variable named "hello". Ex.:
        alert(hello);
        break;
    case 45:
        hello = "hello";
        break;
}

如您所见,我需要在变量获取值之前获取“hello”的值。我不能改变顺序,也不能改变变量声明,所以我必须对这个设置做点什么。

我的想法是在 13 中进行函数调用:

function getMyVariable(variable) {
    var v = variable;
    return variable;
}

case 13:
    getMyVariable(hello);
    break;

那么当然该函数仍将返回未定义。

我想做的是让函数等待45中设置的变量:

(请注意,这只是推测,此代码无法正常工作)

case 45:
    hello = "hello";
    getMyVariable(hello);
    break;

function getMyVariable(variable) {
    //function gets call from case 13
    //if type is undefined, wait for a variable that isn't undefined
    //if variable isn't undefined return the variable
}

所以基本上首先调用skip case 13,让case 45设置变量,然后回到case 13,执行这里的代码。你可以跟我来吗?如果您需要更多信息,请告诉我!

4

4 回答 4

2
// this needs to be defined somewhere outside that it is preserved
// between calls to the code that contains `switch`
var doThisWhenHelloIsSet = [];

// ...
var hello;
switch ($variable) {
    // ...
    case 13:            
        doThisWhenHelloIsSet.push(function(h) {
            // do something with argument named "h" 
            // (which will have the value of variable named "hello").
            // Ex.:
            alert(h);
        });
        break;
    case 45:
        hello = "hello";
        for (var i = 0; i < doThisWhenHelloIsSet.length; i++) {
            doThisWhenHelloIsSet[i](hello);
        }
        doThisWhenHelloIsSet = [];
        break;
}

注意:如果您只想存储一个下一个动作,则不需要数组。

于 2013-07-17T23:38:20.930 回答
0

不完全确定我理解但这是你想要的吗?

var hello = null;
switch ($variable) {
case 2:
    //do something irrelevant
    break;
case 3:
    //do something irrelevant
    break;
case 13:
    //do something with variable named "hello". Ex.:
    if ( hello !== null) {
       alert(hello);
    }
    break;
case 45:
    hello = "hello";
    break;

}

于 2013-07-17T23:37:29.023 回答
0

case您可以将检查与您的hello变量汇总。检查它是否设置检查$variable === 13

var hello;
switch (true) {
    case $variable === 2: 
        // blah
        break;
    case $variable === 3:
        // blah
        break;

    // this will get called when $variable is 13, 
    // and hello is not blank anymore
    case !!hello && $variable === 13:
        // do something magical
        break;

    case $variable === 45:
        // set hello somewhere?
        hello = 'hello';
        break;
}

如果您的意思是同时处理 thecase 13case 45逻辑,则您始终可以尝试失败(尽管要小心)。

var hello;
switch ($variable) {
    case 2 : 
        // blah
        break;
    case 3 :
        // blah
        break;

    // notice I switched these last two up
    case 45 :
        hello = 'hello';
        // notice that there's no break statement
        // so the logic flows into the case 13 right after case 45.

    case 13 :
        if (!!hello) {
            // do something magical with hello
        }
        break;
}
于 2013-07-17T23:46:04.047 回答
-2

您可以使用 for 循环使用标签在 JS 中编写 goto 代码,这些标签在标记 ES5 块时是可定位的。这允许您在没有包装函数和递归的情况下选择性地重复切换(比较和大小写)。

var x=false, y=5;

above: for(;;){ // the GOTO target to jump to mid-switch. 

  switch(y){
   case 3: x=true; break; // done. don't repeat switch 2nd time
   case 5: y=3; continue above; // GOTO back top of switch 
  }

  break above; // don't actually loop
} // end for

alert([x,y]); // shows: true,3

AFAIK,这是“倒退”并使用 oa 函数句柄多次运行代码的唯一方法。

于 2013-07-18T00:14:10.410 回答