A constructor function is a special function used to create and initialize objects. It sets up properties and values for new objects using the new keyword.
Click to reveal answer
beginner
How do you call a constructor function to create a new object?
You call a constructor function with the <code>new</code> keyword before the function name, like <code>const obj = new MyConstructor();</code>. This creates a new object and runs the constructor code.
Click to reveal answer
beginner
What does the this keyword refer to inside a constructor function?
Inside a constructor function, this refers to the new object being created. You use this to add properties or methods to that new object.
Click to reveal answer
intermediate
What happens if you forget to use new when calling a constructor function?
If you forget new, the constructor function runs like a normal function. this will not point to a new object, which can cause errors or unexpected results.
Click to reveal answer
intermediate
How can you add methods to all objects created by a constructor function?
You add methods to the constructor's prototype. For example, MyConstructor.prototype.sayHi = function() { console.log('Hi'); };. All objects created will share this method.
Click to reveal answer
What keyword do you use to create a new object with a constructor function?
Anew
Bcreate
Cobject
Dconstruct
✗ Incorrect
The new keyword creates a new object and runs the constructor function.
Inside a constructor function, what does this refer to?
AThe constructor function itself
BThe global object
CThe new object being created
DUndefined
✗ Incorrect
this points to the new object created by the constructor.
What happens if you call a constructor function without new?
AIt returns null
BIt throws a syntax error
CIt creates a new object anyway
DIt runs as a normal function, <code>this</code> is not the new object
✗ Incorrect
Without new, the function runs normally and this does not refer to a new object.
How do you add a method shared by all objects created by a constructor?
AAdd it to the constructor's prototype
BUse a global variable
CAdd it to each object manually
DAdd it inside the constructor function
✗ Incorrect
Adding methods to the constructor's prototype shares them across all created objects.
Which of these is a correct constructor function name by convention?
Aperson
BPerson
CpersonConstructor
DcreatePerson
✗ Incorrect
Constructor functions usually start with a capital letter to distinguish them.
Explain how a constructor function works in JavaScript and how you create an object with it.
Think about how you make a new object and set its properties.
You got /3 concepts.
Describe how to add a method that all objects created by a constructor function can use.
Where do you put methods so all objects share them?
You got /3 concepts.
Practice
(1/5)
1. What does the new keyword do when used with a constructor function in JavaScript?
easy
A. Creates a new object and sets this to that object inside the constructor
B. Calls the constructor function without creating a new object
C. Deletes the constructor function after running
D. Returns a string representation of the constructor
Solution
Step 1: Understand the role of new
The new keyword creates a fresh object and sets this inside the constructor to that object.
Step 2: Effect on constructor function
It allows the constructor to initialize properties on the new object and then returns it automatically.
Final Answer:
Creates a new object and sets this to that object inside the constructor -> Option A
Quick Check:
new creates object = A [OK]
Hint: Remember: new makes a new object and sets this [OK]
Common Mistakes:
Thinking new just calls the function without creating an object
Believing new returns a string
Confusing new with deleting functions
2. Which of the following is the correct syntax to define a constructor function named Car?
easy
A. function Car() { this.make = 'Toyota'; }
B. var Car = () => { this.make = 'Toyota'; }
C. class Car { make = 'Toyota'; }
D. function Car[] { this.make = 'Toyota'; }
Solution
Step 1: Identify valid constructor function syntax
A constructor function is a normal function with a capitalized name, like function Car() {}.
Step 2: Check each option
function Car() { this.make = 'Toyota'; } uses correct function syntax. var Car = () => { this.make = 'Toyota'; } uses arrow function which does not bind this properly. class Car { make = 'Toyota'; } is a class, not a constructor function. function Car[] { this.make = 'Toyota'; } has invalid syntax with square brackets.
Final Answer:
function Car() { this.make = 'Toyota'; } -> Option A
Quick Check:
Constructor function = normal function with capital name [OK]
Hint: Use normal function with capital name for constructor [OK]
Common Mistakes:
Using arrow functions as constructors
Trying to use class syntax instead of function
Writing invalid function syntax with brackets
3. What will be the output of this code?
function Person(name) {
this.name = name;
}
const p = new Person('Alice');
console.log(p.name);
medium
A. undefined
B. Error
C. Alice
D. Person
Solution
Step 1: Understand object creation with constructor
The new Person('Alice') creates a new object and sets this.name to 'Alice'.
Step 2: Access the property
Logging p.name prints the value assigned, which is 'Alice'.
Final Answer:
Alice -> Option C
Quick Check:
Property set by constructor = 'Alice' [OK]
Hint: Remember: this.name sets property on new object [OK]
Common Mistakes:
Expecting undefined because of missing return
Confusing constructor name with property value
Thinking console.log prints function name
4. Find the error in this constructor function:
function Animal(type) {
type = type;
this.type = type;
}
const a = new Animal('Dog');
console.log(a.type);
medium
A. Missing new keyword when creating object
B. Cannot assign this.type inside constructor
C. Constructor function name should be lowercase
D. The assignment type = type; does nothing and is redundant
Solution
Step 1: Analyze the assignment type = type;
This line assigns the parameter type to itself, which does nothing and is unnecessary.
Step 2: Check other parts
The line this.type = type; correctly assigns the parameter to the object property. The new keyword is used correctly. Function name capitalization is fine.
Final Answer:
The assignment type = type; does nothing and is redundant -> Option D
Quick Check:
Redundant self-assignment = B [OK]
Hint: Check if variables are assigned to themselves unnecessarily [OK]
Common Mistakes:
Thinking type = type; sets object property
Forgetting to use new keyword
Believing constructor names must be lowercase
5. You want to create a constructor function Book that takes title and author as parameters and adds a method describe that returns a string like "Title by Author". Which code correctly does this?
hard
A. function Book(title, author) {
this.title = title;
this.author = author;
}
Book.describe = function() {
return `${this.title} by ${this.author}`;
};
B. function Book(title, author) {
this.title = title;
this.author = author;
this.describe = function() {
return `${this.title} by ${this.author}`;
};
}
C. function Book(title, author) {
this.title = title;
this.author = author;
describe() {
return `${this.title} by ${this.author}`;
}
}
D. function Book(title, author) {
this.title = title;
this.author = author;
this.describe = `${this.title} by ${this.author}`;
}
function Book(title, author) {
this.title = title;
this.author = author;
this.describe = function() {
return `${this.title} by ${this.author}`;
};
} correctly defines describe as a function property on this, so each object has its own method.
Step 2: Analyze other options
function Book(title, author) {
this.title = title;
this.author = author;
}
Book.describe = function() {
return `${this.title} by ${this.author}`;
}; adds describe to the constructor function itself, not instances. function Book(title, author) {
this.title = title;
this.author = author;
describe() {
return `${this.title} by ${this.author}`;
}
} has invalid syntax for method inside function. function Book(title, author) {
this.title = title;
this.author = author;
this.describe = `${this.title} by ${this.author}`;
} assigns a string, not a function, so describe is not callable.
Final Answer:
function Book(title, author) {
this.title = title;
this.author = author;
this.describe = function() {
return `${this.title} by ${this.author}`;
};
} -> Option B
Quick Check:
Method as function property on this = A [OK]
Hint: Define methods as functions on this inside constructor [OK]
Common Mistakes:
Adding methods to constructor function instead of instances
Using invalid syntax for methods inside functions
Assigning method as string instead of function
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.