Может кто-нибудь объяснить мне этот код JavaScript? - PullRequest
0 голосов
/ 28 декабря 2010
// 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");

Как это будет использоваться?

1 Ответ

2 голосов
/ 28 декабря 2010

Это то, как вы можете создавать объекты в javascript, в данном случае, объект UserException с функцией toString. Это может быть использовано так:

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