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();
NotImplementedError.prototype.name = 'NotImplementedError';
(function test() {
function assert(condition, msg) {
if (!condition) {
debugger;
throw new Error('AssertionError: ' + msg);
}
}
function assertEquals(candidate, target, msg) {
var newMsg = ['candidate: "', candidate.toString(), '"; ',
'target: "', target.toString(), '"; ',
msg].join('')
assert(candidate === target, newMsg);
}
var msg = 'too lazy to implement';
var nie = new NotImplementedError(msg);
assertEquals(nie.message, msg, 'Message must be present');
assert(nie instanceof Error, 'Must be Error subclass');
assert(nie instanceof NotImplementedError, 'Must be NotImplementedError instance');
// TODO: assertRaises
})();
A run through js.jar
fails the test:
$ java -jar js.jar test.js
js: "test.js", line 9: exception from uncaught JavaScript throw: Error: AssertionError: candidate: ""; target: "too lazy to implement"; Message must be present
Any ideas as to why, or if there is a better way to create a new Error
subclass? Is there a problem with apply
ing to the native Error
constructor that I don't know about?