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 questionsDomain Objects, Functions, and Classes
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.
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.
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".
Keep practicing
Take the free JAVASCRIPT-DEVELOPER-I practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other JAVASCRIPT-DEVELOPER-I practice questions
- Question 1A developer wants to set up a secure web server withNode.js. The developer creates a dire…
- Question 3Given two expressions var1 and var2. What are two valid ways to return the logical AND of…
- Question 4A test has a dependency on database.query. During the test the dependency is replaced wit…
- Question 6A developer has an ErrorHandler module that contains multiple functions. What kind of exp…
- Question 8Refer to the code below: const event = new CustomEvent( //Missing Code ); obj.dispatchEve…
- Question 9Refer to the code below: Let foodMenu1 = [‘pizza’, ‘burger’, ‘French fries’]; Let finalMe…
