Prepare for the Salesforce JavaScript Developer Exam. Utilize comprehensive quizzes, flashcards, and multiple choice questions with hints and explanations. Boost your exam readiness!

Practice this question and more.


How is the age property accessed if it was set on the instance rather than the prototype?

  1. It will always refer to the prototype's age

  2. It refers to the instance's age property only

  3. TypeError occurs

  4. It cannot be accessed

The correct answer is: It refers to the instance's age property only

The age property can be accessed directly from the instance when it is set on the instance itself. When a property like age is explicitly defined on an instance of an object, it takes precedence over any properties of the same name that may exist on the prototype. This is due to JavaScript's property lookup mechanism, which first checks the instance for the property and only resorts to the prototype if it is not found on the instance. In this case, since the age property is set directly on the instance, accessing it will return the value assigned to that instance's age, confirming option B accurately reflects how the property is accessed. This behavior showcases JavaScript's prototypal inheritance and how it prioritizes instance properties over prototype properties.