Course: JavaScript

Progress (0%)

JavaScript Constructors Quiz

Quiz 17 10 questions

Prerequisites for the quiz

  1. JavaScript Object Constructors

Are you ready?

10 questions to solve

Instructions
  1. This quiz goes to full-screen once you press the Start button.
  2. At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
How to check if a given object o was instantiated using a constructor F()?
The instanceof (all lowercase letters) keyword helps us determine whether a given object was instantiated out of a given constructor. This goes with choice (A). For more details, refer to JavaScript Object Constructors — instanceof.
Suppose that F is a function and that f is an instance of F. What will f.constructor return?
f.constructor returns the constructor function that was used to create f. As is given in the description above, f was instantiated using F, hence f.constructor returns back F. This goes with choice (B). For more details, refer to JavaScript Object Constructors — The constructor property.
What does the following code log?
function Parent() {
   this.a = 0;
}

function Child() {
   Parent();
   this.b = 1;
}

var c = new Child();
console.log(c.a);
When Parent() is called inside the Child() constructor, its this resolves down to the global window object, as it is called without any preceding caller. Hence this.a sets the property a on window, not on the child instance c.

This means that c.a returns undefined, and likewise the correct choice is (C). For more details, refer to JavaScript Object Constructors — Constructor stealing.
What does the following code log?
function Parent() {
   this.a = 0;
   this.b = 0;
}

function Child() {
   this.b = 1;
   new Parent();
}

var c = new Child();
console.log(c.a, c.b);
When new Parent() is called inside the Child() constructor, its this resolves down to the newly-created Parent instance. Hence, both the expressions this.a and this.b inside Parent() add the respective properties to this newly-created Parent instance, not to the Child instance c.

This means that c.a returns undefined while c.b returns 1 (as its set on the first line in the Child() constructor). Likewise, the correct choice is (C). For more details, refer to JavaScript Object Constructors — Constructor stealing.
What does the following code log?
function Parent() {
   this.a = 0;
   this.b = 0;
}

function Child() {
   this.parent = Parent;
   this.parent();
   this.b = 1;
}

var c = new Child();
console.log(c.a, c.b);
When this.parent() is called inside the Child() constructor, the Parent() constructor is invoked with its this set to the child instance being intialized inside Child(). Hence, the properties a and b are set on the instance c. Next up, the statement this.b = 1, inside Child(), executes and updates the property b to the value 1.

Hence, by the time the log statement is executed, c.a is 0 while c.b is 1. Likewise, the correct choice is (B). For more details, refer to JavaScript Object Constructors — Constructor stealing.
We are given the following definition of a constructor function F():
function F() {
   this.a = 0;
}

We want to define a static method s() for this constructor.

What's the correct way to do so?

A static method for a function F is defined directly on F, as shown in choice (B). For more details, refer to JavaScript Object Constructors — Static methods.
What is meant by constructor stealing?
The correct choice is (C). For more details, refer to JavaScript Object Constructors — Constructor stealing.
What does the following code log?
function F() {
   if (this instanceof F) {
      console.log('Yes');
   }
   else {
      console.log('No');
   }
}

new F();
F();
F.call(F);
For the invocation new F(), this is indeed an instance of F, likewise the first log is 'Yes'. For the invocation F(), this points to the window object, likewise it's not an instance of F. Hence, the second log is 'No'. For the invocation F.call(F), this is configured to be F, however, it's also not an instance of F. Likewise, the third log is also 'No'.

Summing all these logs up, the correct choice is (C). For more details, refer to JavaScript Object Constructors — Calling constructors as functions.
It's inefficient to define instance methods as separate properties on every single instance object. True or false?
Definitely, this statement is true. A whole comprehensive discussion can be read at JavaScript Object Constructors — Problem with constructors.
A constructor can accept arguments just like normal functions. True or false?
Indeed, this statement is true. A constructor, in JavaScript, is merely just another function. For more details, refer to JavaScript Object Constructors — What are constructors.

"I created Codeguage to save you from falling into the same learning conundrums that I fell into."

— Bilal Adnan, Founder of Codeguage