Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 217811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:41:32+00:00 2026-05-11T18:41:32+00:00

For some reason it looks like constructor delegation doesn’t work in the following snippet:

  • 0

For some reason it looks like constructor delegation doesn’t work in the following snippet:

function NotImplementedError() { 
  Error.apply(this, arguments); 
}
NotImplementedError.prototype = new Error();

var nie = new NotImplementedError("some message");
console.log("The message is: '"+nie.message+"'")

Running this gives The message is: ''. Any ideas as to why, or if there is a better way to create a new Error subclass? Is there a problem with applying to the native Error constructor that I don’t know about?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:41:32+00:00Added an answer on May 11, 2026 at 6:41 pm

    Update your code to assign your prototype to the Error.prototype and the instanceof and your asserts work.

    function NotImplementedError(message = "") {
        this.name = "NotImplementedError";
        this.message = message;
    }
    NotImplementedError.prototype = Error.prototype;
    

    However, I would just throw your own object and just check the name property.

    throw {name : "NotImplementedError", message : "too lazy to implement"}; 
    

    Edit based on comments

    After looking at the comments and trying to remember why I would assign prototype to Error.prototype instead of new Error() like Nicholas Zakas did in his article, I created a jsFiddle with the code below:

    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);
    }

    The console output was this.

    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
    

    This confirmes the "problem" I ran into was the stack property of the error was the line number where new Error() was created, and not where the throw e occurred. However, that may be better that having the side effect of a NotImplementedError.prototype.name = "NotImplementedError" line affecting the Error object.

    Also, notice with NotImplementedError2, when I don’t set the .name explicitly, it is equal to "Error". However, as mentioned in the comments, because that version sets prototype to new Error(), I could set NotImplementedError2.prototype.name = "NotImplementedError2" and be OK.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, it looks like git cherry-pick pulls in other commits when flies
For some reason git log repo2/master..master -- . doesn't work for me ( fatal:
For some reason, it looks like I cannot pass array of strings as parameter
For some reason, I have following situation: Now I want to get rid of
Could someone please shed some light on this behaviour? It looks like Delphi SOAP
For some reason hibernate is not catching issues like mapping entities to tables that
I have some HTML that looks like this: <div class=product> Content In Here <div
For some reason I never see this done. Is there a reason why not?
For some reason when I attempt to make a request to an Ajax.net web
For some reason when I create a new namespace in Visual Studio 2008 its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.