Doing it this way means you don' t have to change existing code! The fact that it is possible makes Javascript awesome. A few years ago I gave a talk at MountainWest JS called Error Handling in node. Given that Error objects are much better than strings, what if we need to include more than a message with. a lot of easier; class MyError extends Error { } is basically good enough; Override the constructor and use Error. Assuming you' re using Microsoft' s unobtrusive validation, the error message is indeed set for you and will override the message you' re trying to provide in your validator. The message you provide in your validator ( through. function MyError( message) { this. name = ' MyError' ; this. message = message; this. stack = ( new Error( ) ). stack; } MyError. prototype = new Error; / / < - - remove. Test cases I used can be found here: JavaScript self- made Error object comparison.
The message property is a human- readable description of the error. You can set a custom validity message like this $ ( ' [ name= " recipient" ] ' ). on( ' invalid', function( e) { e. setCustomValidity( " This is a custom error message" ) ; } ) ;. Run code snippet. Make sure an error is thrown when attempting to overwrite it / / Without strict- mode, re- assigning will fail silently ' use strict' ; var API = { } ; Object. However, since every function in the JavaScript is a Function object, that Function object is not protected by using writable: false. In Chrome, returning true from window. onerror allows the error to propagate, and returning false suppresses it. This is the inverse of the behavior in Firefox and IE, where returning ' true' suppresses the error, but returning false. You should do much more work for extending Error class: class MyError extends Error { constructor( message) { super( message) ; this. message = message; / / standard way: Error. 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.