下面提供 ES5
和 ES6
两种自定义错误类型的写法
// ES6class CustomError extends Error {constructor(message) {super(message)this.name = 'CustomError'}}// ES5function CustomError(message) {this.name = 'CustomError'this.message = messageError.captureStackTrace(this, CustomError)}CustomError.prototype = new Error()CustomError.prototype.constructor = CustomError
记录错误日志通常使用 <img>
标签, 其可以避免跨域, 用法如下:
function logError(msg) {const img = new Image()img.src = `/log?${encodeURIComponent(msg)}`}