2014-02-20 2 views
0

에서 작동하지 않는,하지만 첫 번째 조건은 노력하고 있습니다 :IF-다른 조건이 코드를 쓴 자바 스크립트

if(document.getElementById('v').value == Infinity) { 
    //alert 
} 
else if(document.getElementById('v').value == -Infinity) { 
    //alert 
}  
else if(document.getElementById('v').value == undefined) { 
    //alert 
} 
else if(document.getElementById('v').value == isNaN) { 
    //alert 
} 

왜이 아닌 다른 조건 (-Infinity, undefined, isNaN) 작동?

+1

어떻게 무한대로 입력 할 수 있습니까? – elclanrs

+0

@elclanrs well 'Infinity'== 강제 변환이 일어나기 때문에 무한대가 참입니다 – axelduch

+0

OP의 유스 케이스 인 경우 문자열과 비교하면 혼란 스럽습니다. – elclanrs

답변

1

throw 키워드로 자바 스크립트에 새 exceptions을 던질 수 있습니다 (아래 예제 참조). 즉 Number.NEGATIVE_INFINITY 또는 Number.POSITIVE_INFINITY

:

if (typeof document.getElementById('v') === 'undefined' || 
    document.getElementById('v').length === 0) 
    throw new TypeError("Undefined value"); 
else if(parseInt(document.getElementById('v').value, 10) == Number.POSITIVE_INFINITY) 
    throw new RangeError("Value is too big"); 
else if(isNaN(document.getElementById('v').value)) 
    throw new TypeError("Value must be a valid number"); 

당신은 같은 논문 예외를 가져올 수

때는 isNaN는

숫자 사용의 유효성을 검사하는 기능입니다 :

try { 
// call your checkup function here 
} catch (exception) { 
alert ("Oh bad... Exception detected: " + exception); 
} 
+0

great! 지금 함수 :) – user3287550

관련 문제