const mark = {
firstName: `Mark`,
lastName: `Miller`,
fullName: this.firstName + this.lastName,
weight: 78,
height: 1.69
};
console.log(mark.fullName); // the result in console: NaN
Asked
Active
Viewed 46 times
0

0stone0
- 34,288
- 4
- 39
- 64
-
`this` refers to the container of the object (window), not the object itself. If you want to create a scope, use a function, or better yet, a class. `window.firstName` is undefined, and in Javascript, `undefined + undefined` gives.......... "not a number" `NaN` because it's trying to add things together, to it assumes it should get a number in the end. – Jeremy Thille Jan 06 '22 at 12:55
1 Answers
0
const mark = {
firstName: `Mark`,
lastName: `Miller`,
fullName: function(){
return this.firstName + this.lastName;
},
weight: 78,
height: 1.69
};
console.log(mark.fullName());
Alternate:
const mark = ({
firstName: `Mark`,
lastName: `Miller`,
init: function(){
this.fullName = this.firstName + this.lastName;
},
weight: 78,
height: 1.69
}).init();
console.log(mark.fullName);

Lakshya
- 1
- 2