Skip to content

Instantly share code, notes, and snippets.

@krazylearner
Created April 29, 2016 13:09
Show Gist options
  • Select an option

  • Save krazylearner/50354c2408da709d0543d9ad2db41320 to your computer and use it in GitHub Desktop.

Select an option

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.
/////////////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