Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A. Like, Raynos said, The reason <code>message</code> isn't being set is that <code>Error</code> is a function that returns a new Error object and does <em>not</em> manipulate <code>this</code> in any way.</p> <p>B. The way to do this right is to return the result of the apply from the constructor, as well as setting the prototype in the usual complicated javascripty way:</p> <pre><code>function MyError() { var tmp = Error.apply(this, arguments); tmp.name = this.name = 'MyError' this.stack = tmp.stack this.message = tmp.message return this } var IntermediateInheritor = function() {} IntermediateInheritor.prototype = Error.prototype; MyError.prototype = new IntermediateInheritor() var myError = new MyError("message"); console.log("The message is: '"+myError.message+"'") // The message is: 'message' console.log(myError instanceof Error) // true console.log(myError instanceof MyError) // true console.log(myError.toString()) // MyError: message console.log(myError.stack) // MyError: message \n // &lt;stack trace ...&gt; </code></pre> <p>The only problems with this way of doing it at this point (i've iteratted it a bit) are that </p> <ul> <li>properties other than <code>stack</code> and <code>message</code> aren't included in <code>MyError</code> and </li> <li>the stacktrace has an additional line that isn't really necessary. </li> </ul> <p>The first problem could be fixed by iterating through all the non-enumerable properties of error using the trick in this answer: <a href="https://stackoverflow.com/questions/8024149/is-it-possible-to-get-the-non-enumerable-inherited-property-names-of-an-object">Is it possible to get the non-enumerable inherited property names of an object?</a>, but this isn't supported by ie&lt;9. The second problem could be solved by tearing out that line in the stack trace, but I'm not sure how to safely do that (maybe just removing the second line of e.stack.toString() ??).</p>
 

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