Skip to content

Instantly share code, notes, and snippets.

@karenpeng
Created May 24, 2015 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karenpeng/78fdf940383c57eae23a to your computer and use it in GitHub Desktop.
Save karenpeng/78fdf940383c57eae23a to your computer and use it in GitHub Desktop.
var animals = [
{ species: 'Lion', name: 'King' },
{ species: 'Whale', name: 'Fail' }
];
for(var i = 0; i < animals.length; i++){
(function(i){
this.print = function(){
console.log(i + ' ' + this.species + ' ' + this.name)
}
this.print()
}).call(animals[i], i)
}
function LateBloomer() {
this.petalCount = Math.ceil(Math.random() * 12) + 1;
}
LateBloomer.prototype.declare = function() {
console.log('ouch!')
console.log('I am a beautiful flower with ' +
this.petalCount + ' petals!')
}
// Declare bloom after a delay of 1 second
LateBloomer.prototype.bloom = function() {
for(var i = 0; i < 10; i ++){
setTimeout(this.declare.bind(this), 1000 * i)
}
}
var a = new LateBloomer()
a.bloom()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment