通常在其他语言中它看起来像 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
关于javascript - 如何作为 null 从类中退出/返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27500840/