如何从类中退出/返回null

时间:2014-12-16 08:59:28

标签: javascript class null exit terminate

通常在其他语言中它看起来像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>

1 个答案:

答案 0 :(得分:3)

是。究竟。您可以抛出异常,说明错误消息

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

More about throw on MDN