Vorremo rendere disponibile questo progetto open-source per persone in tutto il mondo.

Aiutaci a tradurre il contenuto di questo tutorial nella tua lingua!

torna alle lezioni

Eredita da SyntaxError

importanza: 5

Crea una classe FormatError che eredita dalla classe incorporata SyntaxError.

Dovrebbe supportare le proprietà message, name e stack.

Esempio di esecuzione:

let err = new FormatError("formatting error"); alert( err.message ); // Errore nella formattazione alert( err.name ); // FormatError alert( err.stack ); // stack alert( err instanceof FormatError ); // vero alert( err instanceof SyntaxError ); // vero (poiché eredita SyntaxError)
class FormatError extends SyntaxError { constructor(message) { super(message); this.name = this.constructor.name; } } let err = new FormatError("errore di formattazione"); alert( err.message ); // errore di formattazione alert( err.name ); // FormatError alert( err.stack ); // stack alert( err instanceof SyntaxError ); // vero