I have a function which is defined before the object was created. This pre-defined function uses the 'this' keyword to change the value of a property in the object. Inside the object I have a method that calls the predefined method with one argument. However after calling this method and I try to print the value of the property that was supposed to be changed, it still remains the same. How do I fix this?
var setName = function(yourName){
this.name = "Your name is " + yourName;
};
// create an object called `human`
var human = {
name: "Nothing here yet",
setHumanName: function(name) {
setName(name);//Name should be changed now
}
};
human.setHumanName("Emeka");
console.log(human.name); //this does not print the new value of name
setNamemeaning forthisis not what you expect, and you will be getting "invalid property" errors.