Created
May 24, 2015 20:47
-
-
Save karenpeng/78fdf940383c57eae23a to your computer and use it in GitHub Desktop.
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 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