Skip to content

Instantly share code, notes, and snippets.

@schnogz
Last active August 29, 2015 14:25
Show Gist options
  • Save schnogz/c9856cb69fdd3c533022 to your computer and use it in GitHub Desktop.
Save schnogz/c9856cb69fdd3c533022 to your computer and use it in GitHub Desktop.
methods within constructor vs prototype in javascript
function Dog (name, breed) {
// instance variables
this.name = name;
this.breed = breed;
var medicalRecords = {
"hasWorms": true,
"hasDiabetes": false
};
this.checkMedicalRecordsForIssues = function() {
if (medicalRecords.hasWorms || medicalRecords.hasDiabetes) {
return true;
}
return false;
}
}
Dog.prototype.bark = function() {
alert("WOOF!");
};
Dog.prototype.getTrickList = function() {
return [];
};
Dog.prototype.hasMedicalIssues = function() {
return this.checkMedicalRecordsForIssues();
};
function Dog (name, breed) {
// instance variables
this.name = name;
this.breed = breed;
}
Dog.prototype.bark = function() {
alert("WOOF!");
};
Dog.prototype.getTrickList = function() {
return [];
};
// Dog constructor
function Dog (name, breed) {
// instance variables
this.name = name;
this.breed = breed;
// instance functions
this.bark = function() {
alert("WOOF!");
};
this.getTrickList = function() {
return []; // stupid dog
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment