有人能给我解释一下这个JavaScript代码吗?
如何使用它?这是如何在javascript中创建对象的,在本例中,是一个带有有人能给我解释一下这个JavaScript代码吗?,javascript,try-catch,Javascript,Try Catch,如何使用它?这是如何在javascript中创建对象的,在本例中,是一个带有toString函数的UserException对象。可以这样使用: // Create an object type UserException function UserException (message){ this.message=message; this.name="UserException"; } // Make the exception convert to a pre
toString
函数的UserException
对象。可以这样使用:
// Create an object type UserException
function UserException (message){
this.message=message;
this.name="UserException";
}
// Make the exception convert to a pretty string when used as
// a string (e.g. by the error console)
UserException.prototype.toString = function (){
return this.name + ': "' + this.message + '"';
}
// Create an instance of the object type and throw it
throw new UserException("Value too high");
您熟悉抛出异常吗?我试图确定在什么级别解释这个问题。等等,UserException.prototype在这里做什么?
try {
throw new UserException("something went wrong");
} catch(ex) {
console.log(ex);
}