let person = {
firstName: "Rella",
lastName: "binson",
age: 18,
getFullName: function() {
return this.firstName + ' ' + this.lastName;
}
};
for (let key in person) {
if (person.hasOwnProperty(key)) {
console.log(key + ' ' + person[key])
}
}
// it doesn't print 'getFullName()
Consider using getter property:
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])
}
}
Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With