我有以下代码,我循环遍历一个对象并显示其属性值,或者如果它是一个函数则调用该值。
let person = {
fname: "Joseph",
lname: "Michuki",
age: 22,
profession: "developer",
address: "10105550-81 Othaya",
fullname: () => this.fname + " " + this.lname
};
for(i in person){
if(typeof person[i] === 'function'){
console.log(person[i]()); // this logs undefined since the value of this defaults to window which has no fname or lname variables
console.log(person.i); //this results in a type error since i is a string and not a function
console.log(person[i].call(person)); //this logs undefined undefined since the 'this' passed is ignored
}else {
console.log(person[i]);
}
}
我的问题是,我如何参考'姓名'循环中person对象的功能,以便记录正确的值? 谢谢。