javascript - 如何作为 null 从类中退出/返回

标签 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>

最佳答案

是的。确切地。你可以抛出一个异常说明错误信息

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

More about throw on MDN

关于javascript - 如何作为 null 从类中退出/返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27500840/

相关文章:

javascript - Extjs 商店更新事件被延迟触发

java - 按名称访问对象变量和方法

objective-c - 将消息传递为零的开销/风险

javascript - 使用 Underscore(或 Lodash),如何从另一个数组中删除一个数组的每个成员?

javascript - angularjs: Controller 不共享数据,使用服务

javascript - 指示另一个选项卡上的内容何时更改

python - 跨文件组织的 Python 类的可能性?

PHP 多个类的相同命名空间

postgresql - 为什么 postgres 对空值进行分组?

mysql - SQL查询从一列中选择数据,根据年份分成两列,删除空值