考虑使用getter属性:
let person = {
firstName: "Rella",lastName: "binson",age: 18,get getFullName() {
return this.firstName + ' ' + this.lastName;
}
};
for (let key in person) {
if (person.hasOwnProperty(key)) {
console.log(key + ' ' + person[key])
}
}
参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get
,
这是使用现代Object.keys()方法并将其收集到数组中的另一种方法
let person = {
firstName: "Rella",getFullName: function() {
return this.firstName + ' ' + this.lastName;
}
}
const objectMethods = Object.keys(person).filter(item => {
person.hasOwnProperty(item) &&
typeof person[item] === 'function'
})
本文链接:https://www.f2er.com/1788442.html