Created
November 5, 2012 22:10
-
-
Save ricca509/4020717 to your computer and use it in GitHub Desktop.
Constructor invocation
This file contains hidden or 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
var Book = function(title) { | |
this.title = title; | |
} | |
// Let's add a method to Book | |
Book.prototype.getTitle = function() { | |
return this.title; | |
} | |
// Create an instance of Book | |
var myBook = new Book('A noontide blazing'); | |
console.log(myBook.getTitle()); | |
// output: "A noontide blazing" | |
// Now add a new property to Book | |
Book.prototype.pages = 200; | |
// My object inherits the new property even after the instantiation | |
console.log(myBook.pages); | |
// output: 200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment