Object Self Pattern

by karthick6891

HTML

<!-- object self pattern-->

JavaScript

// the self (that, me) pattern

    this.something = "hello1";
    var self = this;
    var callback = function(){
        console.log(self.something);  // hello
    };

    callback();

    
    // --- --- --- --- --- --- --- --- --- 
    // the bind pattern
    
    this.something = "hello2";
    var callback = function(){
        console.log(this.something);  // hello
    }.bind(this);

    callback();


    // --- --- --- --- --- --- --- --- --- 
    // the (new) argument pattern

    this.something = "hello3";

    var callback = function(self){
        console.log(self.something); // hello
    };

    (callback)(this);