Friday, 31 January 2020

Javascript : 'this' keyword inside function and arrow function



This keyword inside normal function and arrow function refers to the global object but if  function is inside of object then within normal function this refers current object and if it's arrow function then 'this' refers to the global object.

var val=500//global variable

var obj={

    val:909,//local variable

    func1:function(){
        console.log(this.val); //909
       // console.log(this); //here this refres obj
    },
    func2:()=>{
        console.log(this.val);//500
        //console.log(this);// here this refers Global(Window object)
     }
}
console.log(obj.func1());
console.log(obj.func2());

No comments:

Post a Comment

JQuery : Quick Review

JQuery Selectors :selectors allows us to find a html elements so, we can manipulate it's attributes programmatically.  Id Sele...