【问题标题】:Can someone explain this JavaScript code to me?有人可以向我解释这段 JavaScript 代码吗?
【发布时间】:2011-05-31 18:23:20
【问题描述】:
// 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");

这将如何使用?

【问题讨论】:

标签: javascript try-catch


【解决方案1】:

这是您在 javascript 中创建对象的方法,在本例中,是带有 toString 函数的 UserException 对象。可以这样使用:

try {
    throw new UserException("something went wrong");
} catch(ex) {
    console.log(ex);
}

【讨论】:

    猜你喜欢
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2011-02-28
    • 1970-01-01
    • 2022-12-16
    • 1970-01-01
    相关资源
    最近更新 更多