Skip to main content
Removed reference to Babel-specific QA post in section that excluded transpilers.
Source Link
  • If you are using ES6 without transpilers:

      class CustomError extends Error { /* ... */}
    
  • If you are using Babel transpiler:

deleted 2 characters in body
Source Link
JBE
  • 12.3k
  • 7
  • 51
  • 52
  • If you are using pure ES5:

      function CustomError(message, fileName, lineNumber) {
        constvar instance = new Error(message, fileName, lineNumber);
        Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
        return instance;
      }
      CustomError.prototype = Object.create(Error.prototype, {
        constructor: {
          value: Error,
          enumerable: false,
          writable: true,
          configurable: true
        }
      });
      if (Object.setPrototypeOf){
          Object.setPrototypeOf(CustomError, Error);
      } else {
          CustomError.__proto__ = Error;
      }
    
  • Alternative: use Classtrophobic framework

  • If you are using pure ES5:

      function CustomError(message, fileName, lineNumber) {
        const instance = new Error(message, fileName, lineNumber);
        Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
        return instance;
      }
      CustomError.prototype = Object.create(Error.prototype, {
        constructor: {
          value: Error,
          enumerable: false,
          writable: true,
          configurable: true
        }
      });
      if (Object.setPrototypeOf){
          Object.setPrototypeOf(CustomError, Error);
      } else {
          CustomError.__proto__ = Error;
      }
    
  • Alternative: use Classtrophobic framework

  • If you are using pure ES5:

      function CustomError(message, fileName, lineNumber) {
        var instance = new Error(message, fileName, lineNumber);
        Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
        return instance;
      }
      CustomError.prototype = Object.create(Error.prototype, {
        constructor: {
          value: Error,
          enumerable: false,
          writable: true,
          configurable: true
        }
      });
      if (Object.setPrototypeOf){
          Object.setPrototypeOf(CustomError, Error);
      } else {
          CustomError.__proto__ = Error;
      }
    
  • Alternative: use Classtrophobic framework

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot
added 146 characters in body
Source Link
JBE
  • 12.3k
  • 7
  • 51
  • 52
Loading
Source Link
JBE
  • 12.3k
  • 7
  • 51
  • 52
Loading