JAVASCRIPT-DEVELOPER-I practice question 5 of 30

Refer to code below: function Person() { this.firstName = ’John’; } Person.prototype ={ Job: x => ‘Developer’ }; const myFather = new Person(); const result=myFather.firstName + ‘ ‘ + myFather.job(); What is the value of the result after line 10 executes?

Full set: 219 questions

Domain Objects, Functions, and Classes

Answer options
A: Error: myFather.job is not a function Correct answer
B: Undefined Developer
C: John undefined
D: John Developer
Correct Answer:
A. Error: myFather.job is not a function
Explanation

JavaScript is a case-sensitive language. The code defines a property Job (with a capital 'J') on the Person.prototype object. However, line 10 attempts to call myFather.job() (with a lowercase 'j'). When myFather.job is accessed, the JavaScript engine first checks the myFather object itself. Finding no job property, it then searches the prototype chain. The prototype object has a property named Job, but not job. Because no property named job is found, the expression myFather.job evaluates to undefined. Attempting to invoke undefined as a function (undefined()) throws a TypeError.

Why Incorrect

B. Undefined Developer: This is incorrect because myFather.firstName is 'John', and more importantly, the code execution halts with a TypeError before any string concatenation can occur.
C. John undefined: This would be the result if myFather.job() returned the value undefined. Instead, the code throws an error when trying to execute undefined as a function.
D. John Developer: This would be the correct output only if the property on the prototype was named job (lowercase), matching the function call. Due to case sensitivity, this is not the case.

References

1. Mozilla Developer Network (MDN) Web Docs, TypeError: "x" is not a function: "The TypeError: "x" is not a function error occurs when there was an attempt to call a value from a function, but the value is not actually a function. ... For example, in JavaScript, case matters. Function names must be called with their correct case." This directly explains the error seen in the question.

Source: MDN Web Docs, TypeError: "x" is not a function, section "Message".

2. Mozilla Developer Network (MDN) Web Docs, Inheritance and the prototype chain: "When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype. ... When trying to access a property of an object, the property will not only be sought on the object but on the prototype of the object, the prototype of the prototype, and so on..." This explains the property lookup mechanism.

Source: MDN Web Docs, Inheritance and the prototype chain, section "Inheriting properties".

3. ECMAScript® 2024 Language Specification (ECMA-262, 15th edition): The specification implicitly defines case sensitivity through its grammar for identifiers (e.g., IdentifierName). It does not treat identifiers with different casing as equivalent.

Source: ECMA-262, 15th Edition, June 2024, Section 12.1, "Identifiers".

Scroll to Top