JS-Dev-101 practice question 4 of 25
01 function Animal(size, type) { 02 this.type = type || 'Animal'; 03 this.canTalk = false; 04 } 05 06 Animal.prototype.speak = function() { 07 if…
Full set: 148 questionsWhen new Pet() is called, the Pet constructor executes. It first calls the Animal constructor via Animal.call(this, ...). The Animal constructor sets two "own" properties on the new instance: this.type (to 'Animal', since the argument was undefined) and this.canTalk (to false). Then, the Pet constructor continues and sets the this.name and this.owner properties (both to undefined). Therefore, canTalk, type, name, and owner are all properties set directly on the pet1 instance. The question is flawed as it asks for three properties when four of the options are correct.
A. speak is a method inherited via the prototype chain (Pet.prototype which is created from Animal.prototype). It is not an "own" property set directly on the pet1 instance.
Note: The question is flawed as it asks for three properties, but B, C, D, and E are all properties set for the pet1 instance during its construction.
1. MDN Web Docs. (2023). new operator. Mozilla. Retrieved from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new. (See section "Description" for the steps of object creation).
2. MDN Web Docs. (2023). Function.prototype.call(). Mozilla. Retrieved from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/GlobalObjects/Function/call. (See section "Using call() to chain constructors for an object").
3. ECMA-262, 13th Edition. (June 2022). ECMAScript® 2022 Language Specification. Section 10.1.13 OrdinaryCreateFromConstructor.
Keep practicing
Take the free JS-Dev-101 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other JS-Dev-101 practice questions
- Question 1A developer executes: document.cookie; document.cookie = 'key=John Smith'; What is the be…
- Question 2A developer is required to write a function that calculates the sum of elements in an arr…
- Question 3Given the code below: 01 const delay = async delay => { 02 return new Promise((resolve, r…
- Question 5Refer to the code snippet: 01 let array = [1, 2, 3, 4, 4, 5, 4, 4]; 02 for (let i = 0; i …
- Question 6Code: 01 let array = [1, 2, 3, 4, 4, 5, 4, 4]; 02 for (let i = 0; i < array.length; i++) …
- Question 7Given a value, which two options can a developer use to detect if the value is NaN?
