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

Ask A Question

Stats

  • Questions 289k
  • Answers 289k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You don't need to configure any settings in order to… May 13, 2026 at 5:28 pm
  • Editorial Team
    Editorial Team added an answer Since the question is tagged c++, I would refactor to:… May 13, 2026 at 5:28 pm
  • Editorial Team
    Editorial Team added an answer This same question was asked over on the Mongo DB… May 13, 2026 at 5:28 pm

Related Questions

For some reason it looks like constructor delegation doesn't work in the following snippet:
I'm a 2nd year ICT student. I never did PHP before this year and
Ok, this question is for people with either a deep knowledge of PRISM or
I have a ViewModel that encapsulates some properties that are being edited in an
First off, I read all of the suggested questions that sounded halfway relevant and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.