Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Update your code to assign your prototype to the Error.prototype and the instanceof and your asserts work.</p> <pre><code>function NotImplementedError(message) { this.name = "NotImplementedError"; this.message = (message || ""); } NotImplementedError.prototype = Error.prototype; </code></pre> <p>However, I would just throw your own object and just check the name property.</p> <pre><code>throw {name : "NotImplementedError", message : "too lazy to implement"}; </code></pre> <p><strong>Edit based on comments</strong></p> <p>After looking at the comments and trying to remember why I would assign prototype to <code>Error.prototype</code> instead of <code>new Error()</code> like Nicholas Zakas did in his <a href="https://www.nczonline.net/blog/2009/03/10/the-art-of-throwing-javascript-errors-part-2/" rel="noreferrer">article</a>, I created a <a href="http://jsfiddle.net/MwMEJ/" rel="noreferrer">jsFiddle</a> with the code below:</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>function NotImplementedError(message) { this.name = "NotImplementedError"; this.message = (message || ""); } NotImplementedError.prototype = Error.prototype; function NotImplementedError2(message) { this.message = (message || ""); } NotImplementedError2.prototype = new Error(); try { var e = new NotImplementedError("NotImplementedError message"); throw e; } catch (ex1) { console.log(ex1.stack); console.log("ex1 instanceof NotImplementedError = " + (ex1 instanceof NotImplementedError)); console.log("ex1 instanceof Error = " + (ex1 instanceof Error)); console.log("ex1.name = " + ex1.name); console.log("ex1.message = " + ex1.message); } try { var e = new NotImplementedError2("NotImplementedError2 message"); throw e; } catch (ex1) { console.log(ex1.stack); console.log("ex1 instanceof NotImplementedError2 = " + (ex1 instanceof NotImplementedError2)); console.log("ex1 instanceof Error = " + (ex1 instanceof Error)); console.log("ex1.name = " + ex1.name); console.log("ex1.message = " + ex1.message); }</code></pre> </div> </div> </p> <p>The console output was this.</p> <pre><code>undefined ex1 instanceof NotImplementedError = true ex1 instanceof Error = true ex1.name = NotImplementedError ex1.message = NotImplementedError message Error at window.onload (http://fiddle.jshell.net/MwMEJ/show/:29:34) ex1 instanceof NotImplementedError2 = true ex1 instanceof Error = true ex1.name = Error ex1.message = NotImplementedError2 message </code></pre> <p>This confirmes the "problem" I ran into was the stack property of the error was the line number where <code>new Error()</code> was created, and not where the <code>throw e</code> occurred. However, that may be better that having the side effect of a <code>NotImplementedError.prototype.name = "NotImplementedError"</code> line affecting the Error object.</p> <p>Also, notice with <code>NotImplementedError2</code>, when I don't set the <code>.name</code> explicitly, it is equal to "Error". However, as mentioned in the comments, because that version sets prototype to <code>new Error()</code>, I could set <code>NotImplementedError2.prototype.name = "NotImplementedError2"</code> and be OK.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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