2014-12-16 6 views
1

보통 널 (null)로 클래스/수익을 종료하지만, 사실 전 C#의 지식을 가진 초보자 오전, JS에서이 작업을 수행하는 방법을 모르는 방법 -하지 JS이 NullReferenceException이 보이는 것이 다른 언어

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

답변

3

예. 정확하게. 당신은 오류 메시지가 나타 예외를 던질 수

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

More about throw on MDN

+0

이 내가보기 나는이 시도 캐치로 지금 노력하고, 전체 페이지에 JS 실행을 중단 실제로 : 그것은 질문 –

+0

을 형성 내 잘못의 뜻 항상 [오류'던짐] (http://www.devthought.com/2011/12/22/a-string-is-not-an-error/)! – Bergi