call method w/Anonymous Function

by js_test

JavaScript

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);
}