Created
July 20, 2012 20:56
-
-
Save jonjaques/3153165 to your computer and use it in GitHub Desktop.
Prototypical Inheritance Pattern
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
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