2017-01-16 3 views
2

JS를 배우려고하는데 객체 속성에 대해 혼란 스럽습니다. 아래 예에서 정의되지 않은 함수를 사용하여 오류를 생성했으며 오류 객체의 속성과 메소드를 얻으려고합니다. e.message는 오류 메시지를 인쇄 할 수 있지만 오류 개체 속성으로 메시지를 가져올 수 없습니다. 여기서 무슨 일이 일어나고있는거야?자바 스크립트 오류 객체 속성

try{ 
    unknownFunction();// undefined function here 
}catch(e){ 
    console.log(e); // it is ReferenceError 
    console.log(e.message);//message 
    console.log(typeof e); // object 
    console.log(e instanceof ReferenceError); // true 
    console.log(e === ReferenceError); // false 
    for(var propertyName in e) { 
    console.log("Name "+propertyName+" and Value "+e[propertyName]); 
    } // [] 
    let allKeys = Object.keys(e); 
    console.log(allKeys);// [] 
    let fnKeys = allKeys.filter(key => typeof myObj[key] == 'function'); 
    console.log(fnKeys); // [] 
} 
+0

repl.it https://repl.it/FKHn/6 –

+0

당신이'message'이 발생할 것으로 예상한다는 것을 의미합니까 'allKeys' 배열? – Xufox

+0

@Xufox 예 당신이 그것을 못 박았습니다, 고마워요! –

답변

0

message 단순히 enumerable property되지 않습니다 : 내가 사용하고

try{ 
    unknownFunction();// undefined function here 
}catch(e){ 
    console.log(Object.getOwnPropertyDescriptor(e, 'message')) 
    /* { 
     value: "unknownFunction is not defined", 
     writable: true, 
     enumerable: false, 
     configurable: true 
    } */ 

    let allKeys = Object.getOwnPropertyNames(e); 
    console.log(allKeys); 
    // ["stack","message"] 

} 
+0

빠른 답변 주셔서 감사합니다, 나는 그것을 계산되지 않습니다하여 답변을 upvote 않았다 :) –

관련 문제