JSFiddle - React, Tailwind, and code Playground

by alexbfree

JavaScript

SomeObject = {
    iterate: function(stuff) {
        _.each(stuff, function() {
            console.log(this);
        });
    }
};

SomeObject.iterate([1,2]); // the same as doing console.log(window)

// _.each() takes a context argument
SomeObject.iterate = function(stuff) {
    _.each(stuff, function() {
        console.log(this);
    }, this);
};

SomeObject.iterate([1,2]); // same as doing console.log(SomeObject)

// _.bind() can also set scope
SomeObject.iterate = function(stuff) {
    var iterator = _.bind(function() {
        console.log(this);
    }, this);
    
    _.each(stuff, iterator);
}

SomeObject.iterate([1,2]); // will also do the equivalent of console.log(SomeObject)