如何以空值退出/返回类



通常在其他语言中,它看起来像NullReferenceException,但我不知道如何在js中做到这一点,实际上我是具有C#知识的初学者 - 不是JS

知识
<script>
    var x = new fooClass(null);
    function fooClass(foo)
    {
        if(foo===null)
            return;     // The return type is "fooClass"
                        // I want to return something that will tell that the
                        // class were wrong initialized, maybe exception?
        alert(foo); // show that the script continues
    }
</script>

是的。完全。您可以抛出一个异常,说明错误消息

if(foo === null)
   throw "null not allowed" // throw an exception with a message

更多关于 MDN 上的throw

最新更新