代码之家  ›  专栏  ›  技术社区  ›  cupakob

相当于php_s die的javascript

  •  84
  • cupakob  · 技术社区  · 15 年前

    在javascript中有类似“死亡”的东西吗?我试过“休息”,但不起作用:)

    12 回复  |  直到 15 年前
        1
  •  28
  •   Eli Grey    15 年前

    你只能 break 一个块作用域,如果你标记它。例如:

    myBlock: {
      var a = 0;
      break myBlock;
      a = 1; // this is never run
    };
    a === 0;
    

    不能从作用域中的函数内中断块作用域。这意味着你不能做这样的事情:

    foo: { // this doesn't work
      (function() {
        break foo;
      }());
    }
    

    您可以使用函数执行类似的操作:

    function myFunction() {myFunction:{
      // you can now use break myFunction; instead of return;
    }}
    
        2
  •  178
  •   Stephen Sorensen    15 年前
    throw new Error("my error message");
    
        3
  •  21
  •   themhz    12 年前

    您可以简单地使用 return; 例子

    $(document).ready(function () {
            alert(1);
            return;
            alert(2);
            alert(3);
            alert(4);
    });
    

    返回将返回到主调用函数test1();并从那里继续到test3();

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
    <script type="text/javascript">
    function test1(){
        test2();
        test3();
    }
    
    function test2(){
        alert(2);
        return;
        test4();
        test5();
    }
    
    function test3(){
        alert(3);
    }
    
    function test4(){
        alert(4);
    }
    
    function test5(){
        alert(5);
    }
    test1();
    
    </script>
    </body>
    </html>
    

    但是,如果您只添加了throw“”;这将完全停止执行,而不会导致任何错误。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
    <script type="text/javascript">
    function test1(){
        test2();
        test3();
    }
    
    function test2(){
        alert(2);
        throw '';   
        test4();
        test5();
    }
    
    function test3(){
        alert(3);
    }
    
    function test4(){
        alert(4);
    }
    
    function test5(){
        alert(5);
    }
    test1();
    
    </script>
    </body>
    </html>
    

    这是用火狐和Chrome测试的。我不知道这是如何由IE或Safari处理的

        4
  •  12
  •   Brilliand    11 年前

    只要打电话 die() 从未定义过。你的脚本将崩溃。:)

    当我这样做的时候,我通常会打电话 discombobulate() 相反,但原理是一样的。

    (实际上,这只不过是扔了一个 ReferenceError ,使其与spully的答案大致相同-但出于调试目的,它的类型较短。)

        5
  •  7
  •   Kelmar    11 年前

    可以滚动您自己版本的PHP模具:

    function die(msg)
    {
        throw msg;
    }
    
    function test(arg1)
    {
        arg1 = arg1 || die("arg1 is missing"); 
    }
    
    test();
    

    JSFiddle Example

        6
  •  5
  •   Mike Szyndel    8 年前

    如果你使用nodejs,你可以使用

    process.exit(<code>);
    
        7
  •  3
  •   nourdine    11 年前

    使用萤火虫和荣耀…

    debugger;
    

    也不要让调试器向前迈任何一步。比扔一个合适的 Error 呢?

        8
  •  2
  •   regulus    9 年前

    语言结构没有完全相等的东西 die 中的php JavaScript . 死亡 在PHP中几乎等于 System.exit() 在里面 爪哇 ,它终止当前脚本并调用关闭挂钩。 如一些用户建议的那样; throw Error 在某些情况下可以使用,但它从不保证终止当前脚本。 在您的 throw 语句-除非在最顶层的脚本块上调用它,否则最终只会退出正在执行的脚本块。

    但是,它不会阻止在此处执行第二个块(打印hello):

    <script type="text/javascript">
      throw new Error('test');
    </script>
    <script type="text/javascript">
      document.write("hello");
    </script> 
    
        9
  •  0
  •   Bugfixer    10 年前

    您可以使用return-false; 这将终止脚本。

        10
  •  0
  •   Matas Lesinskas    6 年前

    这应该像die()一样工作;

    function die(msg = ''){
        if(msg){
            document.getElementsByTagName('html')[0].innerHTML = msg;
        }else{
            document.open();
            document.write(msg);
            document.close();
        }
        throw msg;
    }
    
        11
  •  0
  •   Yassine CHABLI    6 年前

    您可以尝试:

    return 0;
    

    这在停止过程中起作用。

        12
  •  -4
  •   Koby Douek Cicero Silva Luiz Junior    7 年前
    <script>
         alert("i am ajith fan");
         <?php die(); ?>
         alert("i love boxing");
         alert("i love MMA");
    </script>