Last active
August 29, 2015 14:25
-
-
Save schnogz/c9856cb69fdd3c533022 to your computer and use it in GitHub Desktop.
methods within constructor vs prototype in javascript
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 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(); | |
}; |
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 Dog (name, breed) { | |
// instance variables | |
this.name = name; | |
this.breed = breed; | |
} | |
Dog.prototype.bark = function() { | |
alert("WOOF!"); | |
}; | |
Dog.prototype.getTrickList = function() { | |
return []; | |
}; |
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
// 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