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