0

I'm slowly working my way to understanding the subtleties of JavaScript. At this time, I'm trying to instantiate an instance of the JavaScript Error object. I know that I can do the following:

var error = new Error('The value is invalid');
error.name = 'ArgumentException';
throw error;

However, I'm trying to figure out/understand if there's a way to condense this instantiation down to a single line. For instance, I'd like to do something like the following:

var error = new Error().{ message:'The value is invalid', name:'ArgumentException'};
throw error;

Is it possible to instantiate an instance of the JavaScript Error object while setting both the message and name properties?

Thank you!

JQuery Mobile
  • 6,221
  • 24
  • 81
  • 134
  • The `name` property is inherited from `Function`, so you can't set it directly when instanciating an `Error` object as far as I know. – adeneo Jan 19 '14 at 16:05
  • Why would condensing this to a single line make it better? Take a look at the documentation https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error – Adam Jenkins Jan 19 '14 at 16:05
  • 2
    For a one-liner, closest you'll get is something like `throw (function() { var e = new Error("Whoops!"); e.name = 'ArgumentException'; return e; })();` – adeneo Jan 19 '14 at 16:06
  • It's just a preference to get it to a single line. Plus, I'm trying to learn more about the JavaScript language. – JQuery Mobile Jan 19 '14 at 16:07
  • http://jsfiddle.net/kt5TR/2/ – adeneo Jan 19 '14 at 16:07
  • A note on your code in a larger context: considering the name you want to set - ArgumentException - taking @roland's answer as an example, I think creating your own Error class would be a good thing. I would often do such a thing to help out with later (inevitable) revisions, rather than quick code right now. However, when comparing performance against time taken to write code, maybe it's not so good. – Henry Blyth Jan 19 '14 at 23:19

3 Answers3

1

You can assign error.name on the same line as throwing it.

If the right hand side of an assignment is a truthy value, which yours is (a non-empty string), the assignment statement will be truthy, meaning you can run on with &&. The last truthy statement will be sent back to throw at the beginning of the line and will be thrown.

var error = new Error('The value is invalid');
throw (error.name = 'ArgumentException') && error;

Without creating your own Error class with name set to ArgumentException, this is as few statements as you can get to without using an Immediately Invoked Function Expression (IIFE) to have one block statement.

EDIT: adeneo correctly used an IIFE which is one statement, even though it contains multiple statements within it as a block. I've edited my last paragraph to include this.

Henry Blyth
  • 1,700
  • 1
  • 15
  • 23
1

If you really want everything on a single line, you can do something like

var error = Object.defineProperty(new Error('The value is invalid'), 'name', {value: 'ArgumentException'});

You could also throw it on the same line

throw Object.defineProperty(new Error('The value is invalid'), 'name', {value: 'ArgumentException'});

I'm not really fond of how it looks like, but I guess this answers your question.

Hugo Chevalier
  • 276
  • 1
  • 3
  • 6
0

I created a custom Error object a while ago following along Mozilla recommendations. Basically it creates an object in an OO fashion (pay attention to prototype and constructor). Caution there are some limitations (type-specific catches; read the resources I mentioned below):

// Create a new object, that prototypilly inherits from the Error constructor.
function MyError(message) {
  this.name = "MyError";
  this.message = message || "Default Message";
}
MyError.prototype = new Error(); 
MyError.prototype.constructor = MyError;

try {
  throw new MyError();
} catch (e) {
  console.log(e.name);     // "MyError"
  console.log(e.message);  // "Default Message"
}

try {
  throw new MyError("custom message");
} catch (e) {
  console.log(e.name);     // "MyError"
  console.log(e.message);  // "custom message"
}

For further information: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Error

The Mozilla page refers to the following question (and caveats you must be aware of): What's a good way to extend Error in JavaScript?

Community
  • 1
  • 1
roland
  • 7,695
  • 6
  • 46
  • 61
  • This is good for a larger code base. A linked answer that SO displayed in the sidebar adds calling the original class in the constructor, and IMO is worth reading: http://stackoverflow.com/a/1382129/3150057 – Henry Blyth Jan 19 '14 at 23:21