覆盖javascript的Error对象

时间:2018-02-21 15:45:03

标签: javascript override dry

我想,每次捕获错误(在javascript中)都会执行特定操作 (在下面的示例中,操作是将消息写在'p'标记中)

我写了一个测试代码,当我创建错误(实例化)时它运行得很好

当系统创建错误

时,代码不会运行

谁知道为什么? (好像系统在不使用构造函数的情况下获取错误对象的实例)

下面是我使用的代码!

<!doctype html>
<html lang="us">
<head>
    <meta charset="utf-8">
    <title>RE</title>

</head>
<body>

<input type="button" value="Error Thrown"  onclick="btnErrorThrown_click()">

<input type="button" value="Error Catched"  onclick="btnErrorCatched_click()">

<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" target="_blank">Error - JavaScript | MDN</a></p>

<p id="pMessage" style="color:red" onclick="pMessage_click(this)">&nbsp;</p>

<script>

    // -------------------------------------------------------------- //
    // The overriding of the constructor!
    originalReferenceErrorPrototype = ReferenceError.prototype;
    ReferenceError = function (message){
        pMessageWrite(message);
        return originalReferenceErrorPrototype.constructor(message);
    }
    ReferenceError.prototype = originalReferenceErrorPrototype;
    // -------------------------------------------------------------- //


    function btnErrorThrown_click()
    {
        try{    
            //var re = new ReferenceError('Error Thrown');
            //var re = ReferenceError('Error Thrown');
            throw new ReferenceError('Error Thrown');

            // All the three cases above run correctly 
        }
        catch(jse){
            checkType(jse);
            alert(jse.message);        
        }
    }


    function btnErrorCatched_click()
    {
        try{
            eval('var x = q;');
            // this code raises a 'ReferenceError' but the override function isn't called
            // WHY??????????

        }
        catch(jse){
            checkType(jse);
            alert(jse.message);
        }
    }

    // this solution works only for not managed errors!
    // Errors not included in a 'try-catch' session.
    window.onerror = function(message, source, lineno, colno, error) {
        alert('window.onerror: ' + message);
    }




    // infrastuctural code: not important ============================== //

    // Used to check if the error is of the right type.
    // If the override is not correct the type of the catched variable is 'object'
    function checkType(jse){
        if(false){}
        else if (jse instanceof EvalError)      { alert('EvalError');}
        else if (jse instanceof RangeError)     { alert('RangeError');}
        else if (jse instanceof ReferenceError) { alert('ReferenceError');}
        else if (jse instanceof SyntaxError)    { alert('SyntaxError');}
        else if (jse instanceof TypeError)      { alert('TypeError');}
        else if (jse instanceof URIError)       { alert('URIError');}
        else if (jse instanceof Error)          { alert('Error');}
        else{ alert(typeof jse);}
    }

    // writes the message in the 'p' tag and clear it after one second
    function pMessageWrite(message){
        var pMessage = document.getElementById("pMessage");
        pMessage.innerText = message;
        setTimeout(function(){pMessage_click(pMessage);}, 1000);
    }

    // remove the message from the 'p' tag
    function pMessage_click(p){
        p.innerText = '';
    }
    // infrastuctural code =================================================== //


</script>
</body>
</html>

0 个答案:

没有答案