[英]Can someone explain this JavaScript code to me?
// 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");
How will this be used? 如何使用?
This is how you can create objects in javascript, in this case, a UserException
object with a toString
function. 这是您可以在javascript中创建对象的方法,在这种情况下,该对象是带有
toString
函数的UserException
对象。 It might be used like so: 它可能像这样使用:
try {
throw new UserException("something went wrong");
} catch(ex) {
console.log(ex);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.