通常在其他语言中它看起来像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>
答案 0 :(得分:3)
是。究竟。您可以抛出异常,说明错误消息
if(foo === null)
throw "null not allowed" // throw an exception with a message