JS-Dev-101 practice question 10 of 25

Refer to the following object: 01 const cat = { 02 firstName: 'Fancy', 03 lastName: 'Whiskers', 04 get fullName(){ 05 return this.firstName + ' ' + this.lastName; 06 } 07 }; How can a developer access the fullName property for cat?

Full set: 148 questions
Answer options
A: cat.fullName()
B: cat.get.fullName
C: cat.function.fullName()
D: cat.fullName Correct answer
Correct Answer:
D. cat.fullName
Explanation

The code defines a getter for the fullName property. A getter binds an object property to a function that is executed when that property is looked up. It is accessed like a standard property, without parentheses, which invokes the underlying function and returns its result. Calling it as a function cat.fullName() would result in a TypeError because the property's value is the string returned by the getter, not the function itself.

Why Incorrect

A. cat.fullName() attempts to call the property as a method, which is incorrect for a getter and will cause a TypeError.
B. cat.get.fullName is invalid syntax; get is a keyword for defining the accessor, not a property on the object instance.
C. cat.function.fullName() is invalid syntax; function is not a property on the cat object that would contain fullName.

References

1. MDN Web Docs. (2023). get - JavaScript | MDN. Mozilla. Retrieved from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get. (See "Syntax" and "Examples" sections).

2. ECMA-262, 13th Edition. (June 2022). ECMAScript® 2022 Language Specification. Section 13.2.6.4 Object Literal: MethodDefinition.

Scroll to Top