this-case 1-Fix this when used in a method passed as a callbac

Fix this when used in a method passed as a callbac http://javascriptissexy.com/understand-javascripts-this-with-clarity-and-master-it/

by shenoyvnm

HTML

<input type="button" id="filter" name="button" value="Filter" />

JavaScript

/* There are two things you need to see.
First when you call myObject.showData, if you 
have a) $ ("#filter").click(myObject.showData());
since you have () at end of showData i.e (myObject.showData()), showData will be called on pageLoad and in this case,"this" will refer to data only. i.eif you print this on console you will see below
Object {name: "maju", data: Array[2], showData: function}.

On the other hand 
b)if ("#filter").click(myObject.showData), here showData has no paranethesis, therefore it will be called only when you click Button. In that case, this will be 
<input type="button" id="filter" name="button" value="Filter">
*/


var myObject={
    name:"maju",
    data:[{name:"raju",id:101},{name:"mahesh",id:102}],
    showData:function(){
        debugger;
        var randomNumer=((Math.random()*2|0)+1)-1;
        console.log("name is"+this.data[randomNumer].name+"age is"+this.data[randomNumer].id);
    }
}

 $ ("#filter").click(myObject.showData);
//fix is
/*$("button").click (user.clickHandler.bind (user)); // P. Mickelson 43*/