我有一个在创建对象之前定义的函数。此预定义函数使用“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