I am new to JS and like other novice struggling to understand concept of THIS keyword in JS. In below snippet name is changed to value1 which I understand since its c object which is calling log function .
But not able to understand why setName function is not able to change value of name variable?
var c ={
name:"value",
log:function(){
this.name="Value 1";
console.log(this.name); // c object is invoking log function hence this refers to c obj
var setName= function(newName){
this.name=newName;
}
setName('Value 2'); // isn't log function calling setName
console.log(this.name);
}
}
c.log();