Note that there are some explanatory texts on larger screens.

plurals
  1. POExtending the native JavaScript Error constructor
    primarykey
    data
    text
    <p>I tried to extend the JavaScript error properties through the extension of the prototype of the Error constructor:</p> <pre><code>&lt;script type="text/javascript"&gt; // extending the Error properties to all the ones available in the various browsers: Error.prototype = { name: null, // (all browsers); the name of the error message: null, // (all browsers); the error message as a string description: null, // (Internet Explorer); description of the error fileName: null, // (Firefox); the name of the file where the error occurred lineNumber: null, // (Firefox); the number of line where the error occurred columnNumber: null, // (Firefox); the number of column where the error occurred number: null, // (Internet Explorer); the error code as a number stack: null // (Firefox, Chrome); detailed information about the location where the error exactly occurred }; function log(error) { var errors = []; for (var prop in error) { errors.push(prop + ': ' + error[prop]); } alert(errors.join('\n')); } &lt;/script&gt; </code></pre> <p>Then I test the log function:</p> <pre><code>&lt;script type="text/javascript&gt; try { var a = b; // b is undefined! } catch(error) { log(error); } &lt;/script&gt; </code></pre> <p>The result is that the error object only shows some properties (e.g. on Firefox <code>fileName</code>, <code>lineNumber</code> and <code>columnNumber</code>) like if it has not been extended.</p> <p>But the most strange thing is that the <code>for...in</code> cycle seems <strong>unable to walk all the error object properties</strong>: trying to alert the standard property <code>error.message</code> normally returns a message.</p> <p>So the results of my test are:</p> <ol> <li>the Error constructor is not extensible through its prototype, as other native constructors are;</li> <li>the <code>for...in</code> loop is not able to walk the properties of an error object.</li> </ol> <p>Am I right?<br /> Are there some interesting evidences/resources you may suggest to know more about it?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload