Created
April 29, 2016 13:09
-
-
Save krazylearner/50354c2408da709d0543d9ad2db41320 to your computer and use it in GitHub Desktop.
A drawback of 1.1. is that the method getInfo() is recreated every time you create a new object. Sometimes that may be what you want, but it's rare. A more inexpensive way is to add getInfo() to the prototype of the constructor function.
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
| /////////////antipattern/////////// | |
| function Apple (type) { | |
| this.type = type; | |
| this.color = "red"; | |
| this.getInfo = function() { | |
| return this.color + ' ' + this.type + ' apple'; | |
| }; | |
| } | |
| ///////////CORRECT/////////// | |
| function Apple (type) { | |
| this.type = type; | |
| this.color = "red"; | |
| } | |
| Apple.prototype.getInfo = function() { | |
| return this.color + ' ' + this.type + ' apple'; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment