Skip to content

Instantly share code, notes, and snippets.

@SeanTRobinson
Created September 10, 2014 09:00
Show Gist options
  • Save SeanTRobinson/3def573da5be5202c7b8 to your computer and use it in GitHub Desktop.
Save SeanTRobinson/3def573da5be5202c7b8 to your computer and use it in GitHub Desktop.
A scope safe object creation example with a clean prototype manipulation.
function Book(isbn, title, author) {
if (this instanceof Book) {
this.isbn = isbn;
this.title = title;
this.author = author;
} else {
return new Book(isbn, title, author);
}
}
Book.prototype = {
constructor: Book,
toString: function () {
return "[Book: {isbn: " + this.isbn + ", title: " + this.title + ", author: " + this.author + "}]";
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment