Created
September 10, 2014 09:00
-
-
Save SeanTRobinson/3def573da5be5202c7b8 to your computer and use it in GitHub Desktop.
A scope safe object creation example with a clean prototype manipulation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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