| Refresh | Home EGTry.com

javascript 'this', not where it define, but what context it is executed


commentsJavascript sourceExecution result in your current browser
global execution context

  document.write("global this="+this);
  

inside literal object
 var obj1={
     name:"object 1",
     f: function() {
        document.write("call inside object: "+this.name);
         }
 };

  obj1.f();

resign the same function to another object
   var obj2={name:"object 2", f:obj1.f};
   obj2.f();
 

   this.name="global1";
   var globalFunc=obj2.f;
    globalFunc();