Skip to content

Instantly share code, notes, and snippets.

@jonjaques
Created July 20, 2012 20:56
Show Gist options
  • Save jonjaques/3153165 to your computer and use it in GitHub Desktop.
Save jonjaques/3153165 to your computer and use it in GitHub Desktop.
Prototypical Inheritance Pattern
var Dog = function(name){
var name = name,
numberOfBarks = function(volume){
for(var i = 10; volume >= i; i--){
if(volume % 3 === 0){
return volume / 3;
}
}
},
barkWithVolume = function(volume){
var barks = numberOfBarks(volume),
barkArray = [];
for(var i = 0; i < barks; i++){
barkArray.push('Bark');
}
return (name + ': ' + barkArray.join(' ') + '!!!');
};
return {
name: name,
bark: barkWithVolume
}
};
var sparky = new Dog('Sparky'); // { name: 'Sparky', bark: function(volume){} }
sparky.bark(10); // "Sparky: Bark Bark Bark!!!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment