Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

A. Like, Raynos said, The reason message isn't being set is that Error is a function that returns a new Error object and does not manipulate this in any way.

B. The way to do this right is to set the result of the apply from the constructor on this, as well as setting the prototype in the usual complicated javascripty way:

function MyError() { var tmp = Error.apply(this, arguments) tmp.name = this.name = 'MyError' this.message = tmp.message // instead of this.stack = ..., a getter for more optimizy goodness Object.defineProperty(this, 'stack', { get: function () { return tmp.stack } }) return this } var IntermediateInheritor = function () {} IntermediateInheritor.prototype = Error.prototype MyError.prototype = new IntermediateInheritor() var myError = new MyError("message") console.log("The message is: '"+myError.message+"'") // The message is: 'message' console.log(myError instanceof Error) // true console.log(myError instanceof MyError) // true console.log(myError.toString()) // MyError: message console.log(myError.stack) // MyError: message \n // <stack trace ...> 

The only problems with this way of doing it at this point (i've iteratted it a bit) are that

  • properties other than stack and message aren't included in MyError, and
  • the stacktrace has an additional line that isn't really necessary.

The first problem could be fixed by iterating through all the non-enumerable properties of error using the trick in this answer: Is it possible to get the non-enumerable inherited property names of an object?Is it possible to get the non-enumerable inherited property names of an object?, but this isn't supported by ie<9. The second problem could be solved by tearing out that line in the stack trace, but I'm not sure how to safely do that (maybe just removing the second line of e.stack.toString() ??).

Update

I created an inheritance library that does this ^ https://github.com/fresheneesz/proto

A. Like, Raynos said, The reason message isn't being set is that Error is a function that returns a new Error object and does not manipulate this in any way.

B. The way to do this right is to set the result of the apply from the constructor on this, as well as setting the prototype in the usual complicated javascripty way:

function MyError() { var tmp = Error.apply(this, arguments) tmp.name = this.name = 'MyError' this.message = tmp.message // instead of this.stack = ..., a getter for more optimizy goodness Object.defineProperty(this, 'stack', { get: function () { return tmp.stack } }) return this } var IntermediateInheritor = function () {} IntermediateInheritor.prototype = Error.prototype MyError.prototype = new IntermediateInheritor() var myError = new MyError("message") console.log("The message is: '"+myError.message+"'") // The message is: 'message' console.log(myError instanceof Error) // true console.log(myError instanceof MyError) // true console.log(myError.toString()) // MyError: message console.log(myError.stack) // MyError: message \n // <stack trace ...> 

The only problems with this way of doing it at this point (i've iteratted it a bit) are that

  • properties other than stack and message aren't included in MyError, and
  • the stacktrace has an additional line that isn't really necessary.

The first problem could be fixed by iterating through all the non-enumerable properties of error using the trick in this answer: Is it possible to get the non-enumerable inherited property names of an object?, but this isn't supported by ie<9. The second problem could be solved by tearing out that line in the stack trace, but I'm not sure how to safely do that (maybe just removing the second line of e.stack.toString() ??).

Update

I created an inheritance library that does this ^ https://github.com/fresheneesz/proto

A. Like, Raynos said, The reason message isn't being set is that Error is a function that returns a new Error object and does not manipulate this in any way.

B. The way to do this right is to set the result of the apply from the constructor on this, as well as setting the prototype in the usual complicated javascripty way:

function MyError() { var tmp = Error.apply(this, arguments) tmp.name = this.name = 'MyError' this.message = tmp.message // instead of this.stack = ..., a getter for more optimizy goodness Object.defineProperty(this, 'stack', { get: function () { return tmp.stack } }) return this } var IntermediateInheritor = function () {} IntermediateInheritor.prototype = Error.prototype MyError.prototype = new IntermediateInheritor() var myError = new MyError("message") console.log("The message is: '"+myError.message+"'") // The message is: 'message' console.log(myError instanceof Error) // true console.log(myError instanceof MyError) // true console.log(myError.toString()) // MyError: message console.log(myError.stack) // MyError: message \n // <stack trace ...> 

The only problems with this way of doing it at this point (i've iteratted it a bit) are that

  • properties other than stack and message aren't included in MyError, and
  • the stacktrace has an additional line that isn't really necessary.

The first problem could be fixed by iterating through all the non-enumerable properties of error using the trick in this answer: Is it possible to get the non-enumerable inherited property names of an object?, but this isn't supported by ie<9. The second problem could be solved by tearing out that line in the stack trace, but I'm not sure how to safely do that (maybe just removing the second line of e.stack.toString() ??).

Update

I created an inheritance library that does this ^ https://github.com/fresheneesz/proto

Please don't add useless crap to my answer
Source Link
B T
  • 61.6k
  • 36
  • 200
  • 213
function MyError() { var tmp = Error.apply(this, arguments); tmp.name = this.name = 'MyError';'MyError' this.message = tmp.message;message // instead of this.stack = ..., a getter for more optimizy goodness Object.defineProperty(this, 'stack', { get: function () { return tmp.stack;stack } }); return this;this } var IntermediateInheritor = function () {}; IntermediateInheritor.prototype = Error.prototype;prototype MyError.prototype = new IntermediateInheritor(); var myError = new MyError("message"); console.log("The message is: '" + myError'"+myError.message + "'"message+"'"); // The message is: 'message' console.log(myError instanceof Error); // true console.log(myError instanceof MyError); // true console.log(myError.toString()); // MyError: message console.log(myError.stack); // MyError: message \n // <stack trace ...> 
function MyError() { var tmp = Error.apply(this, arguments); tmp.name = this.name = 'MyError'; this.message = tmp.message; // instead of this.stack = ..., a getter for more optimizy goodness Object.defineProperty(this, 'stack', { get: function () { return tmp.stack; } }); return this; } var IntermediateInheritor = function () {}; IntermediateInheritor.prototype = Error.prototype; MyError.prototype = new IntermediateInheritor(); var myError = new MyError("message"); console.log("The message is: '" + myError.message + "'"); // The message is: 'message' console.log(myError instanceof Error); // true console.log(myError instanceof MyError); // true console.log(myError.toString()); // MyError: message console.log(myError.stack); // MyError: message \n // <stack trace ...> 
function MyError() { var tmp = Error.apply(this, arguments) tmp.name = this.name = 'MyError' this.message = tmp.message // instead of this.stack = ..., a getter for more optimizy goodness Object.defineProperty(this, 'stack', { get: function () { return tmp.stack } }) return this } var IntermediateInheritor = function () {} IntermediateInheritor.prototype = Error.prototype MyError.prototype = new IntermediateInheritor() var myError = new MyError("message") console.log("The message is: '"+myError.message+"'") // The message is: 'message' console.log(myError instanceof Error) // true console.log(myError instanceof MyError) // true console.log(myError.toString()) // MyError: message console.log(myError.stack) // MyError: message \n // <stack trace ...> 
code formatted and highlighted, added semicolons, updated code comments for readability
Source Link
Eliran Malka
  • 16.4k
  • 7
  • 80
  • 103
function MyError() { var tmp = Error.apply(this, arguments); tmp.name = this.name = 'MyError' this.message = tmp.message /*this.stack = */Object.defineProperty(this, 'stack', { // getter for more optimizy goodness get: function() { return tmp.stack } }) return this } var IntermediateInheritor = function() {} IntermediateInheritor.prototype = Error.prototype; MyError.prototype = new IntermediateInheritor() var myError = new MyError("message"); console.log("The message is: '"+myError.message+"'") // The message is: 'message' console.log(myError instanceof Error) // true console.log(myError instanceof MyError) // true console.log(myError.toString()) // MyError: message console.log(myError.stack) // MyError: message \n // <stack trace ...> 
function MyError() { var tmp = Error.apply(this, arguments); tmp.name = this.name = 'MyError'; this.message = tmp.message; // instead of this.stack = ..., a getter for more optimizy goodness Object.defineProperty(this, 'stack', { get: function () { return tmp.stack; } }); return this; } var IntermediateInheritor = function () {}; IntermediateInheritor.prototype = Error.prototype; MyError.prototype = new IntermediateInheritor(); var myError = new MyError("message"); console.log("The message is: '" + myError.message + "'"); // The message is: 'message' console.log(myError instanceof Error); // true console.log(myError instanceof MyError); // true console.log(myError.toString()); // MyError: message console.log(myError.stack); // MyError: message \n // <stack trace ...> 
function MyError() { var tmp = Error.apply(this, arguments); tmp.name = this.name = 'MyError' this.message = tmp.message /*this.stack = */Object.defineProperty(this, 'stack', { // getter for more optimizy goodness get: function() { return tmp.stack } }) return this } var IntermediateInheritor = function() {} IntermediateInheritor.prototype = Error.prototype; MyError.prototype = new IntermediateInheritor() var myError = new MyError("message"); console.log("The message is: '"+myError.message+"'") // The message is: 'message' console.log(myError instanceof Error) // true console.log(myError instanceof MyError) // true console.log(myError.toString()) // MyError: message console.log(myError.stack) // MyError: message \n // <stack trace ...> 
function MyError() { var tmp = Error.apply(this, arguments); tmp.name = this.name = 'MyError'; this.message = tmp.message; // instead of this.stack = ..., a getter for more optimizy goodness Object.defineProperty(this, 'stack', { get: function () { return tmp.stack; } }); return this; } var IntermediateInheritor = function () {}; IntermediateInheritor.prototype = Error.prototype; MyError.prototype = new IntermediateInheritor(); var myError = new MyError("message"); console.log("The message is: '" + myError.message + "'"); // The message is: 'message' console.log(myError instanceof Error); // true console.log(myError instanceof MyError); // true console.log(myError.toString()); // MyError: message console.log(myError.stack); // MyError: message \n // <stack trace ...> 
added 194 characters in body
Source Link
B T
  • 61.6k
  • 36
  • 200
  • 213
Loading
added 7 characters in body
Source Link
B T
  • 61.6k
  • 36
  • 200
  • 213
Loading
oops nevermind
Source Link
B T
  • 61.6k
  • 36
  • 200
  • 213
Loading
added 104 characters in body
Source Link
B T
  • 61.6k
  • 36
  • 200
  • 213
Loading
added 49 characters in body
Source Link
B T
  • 61.6k
  • 36
  • 200
  • 213
Loading
Source Link
B T
  • 61.6k
  • 36
  • 200
  • 213
Loading