I was using vi editor on my mac terminal and writing some Javascript codes. When i used Spidermonkey Engine to run on this code:
function getPerson(id) {
if (id < 0) {
throw new Error('ID must not be negative: '+id);
}
return { id: id };
}
function getPersons(ids) {
var result = [];
ids.forEach(function (id) {
try {
var person = getPerson(id);
result.push(person);
} catch (exception) {
print(exception);
}
});
return result;
}
When i run the following commands:
js -f exc.js -i
js> getPersons([1,-2,3]);
I get the following response:
Error: ID must not be negative: -2
[object Object],[object Object]
Instead of:
Error: ID must not be negative: -2
{ id: 1 }, { id: 3 }
So, how am i supposed to correct this?
If you have control on where the object is printed, try using JSON.stringify
on the object before printing it.
Possibly this may work (not tested):
js> JSON.stringify(getPersons([1,-2,3]));