Function Invocation Pattern

From JS;TGP. Look out for this ♬ and-uh ♬ that and-uh ♬

by Steven Senkus

JavaScript

var myObject = {
    value: 0,
    name: (Math.random() * 100).toFixed(2),
    increment: function(inc) {
        this.value += typeof inc === 'number' ? inc : 1;
        this.log();
    },
    log: function() {
        console.log(this.name + ' value', this.value  )
    }
};

myObject.double = function() {
    var that = this;
    var helper = (function() {
        that.value = that.value * 2;
        that.log();
    })();
    var derp = function() { // this is bound to the global object!!!
        console.log(this);
    };
    derp();
};

myObject.log();
myObject.increment(10)
myObject.log();
myObject.double()