Which keyword is used to define a constructor function in JavaScript?
Analysis & Theory
`function` is used to define a constructor function in JavaScript.
What does the `new` keyword do in JavaScript?
B
Creates a new object instance
C
Calls a function normally
Analysis & Theory
`new` is used to create a new instance of an object using a constructor function.
Which of the following creates an object using a constructor function?
B
let obj = new Person();
D
let obj = Person.new();
Analysis & Theory
`new Person()` creates a new object using the `Person` constructor function.
In a constructor function, what does `this` refer to?
C
The newly created object
Analysis & Theory
Inside a constructor function, `this` refers to the new object being created.
What is the default return value of a constructor function?
Analysis & Theory
By default, a constructor returns the newly created object referenced by `this`.
Which is a valid constructor function?
A
function Car(color) { this.color = color; }
B
let Car = { color: 'red' };
C
function car(color) = { this.color = color; };
D
Car(color) { this.color = color; }
Analysis & Theory
`function Car(color) { this.color = color; }` is the correct syntax for a constructor function.
What is the purpose of capitalizing constructor function names (e.g., `Person`) in JavaScript?
Analysis & Theory
Capitalizing constructor names is a convention to indicate that the function is meant to be used with `new`.
How can you add a method to all instances of a constructor?
B
Using `this.method` inside a loop
C
By modifying the prototype
Analysis & Theory
You can use `Constructor.prototype.methodName = function() {}` to add methods to all instances.
Which of the following adds a shared property to all instances of a constructor?
C
Car.prototype.name = 'Alex';
Analysis & Theory
Using `Car.prototype.name = 'Alex';` adds a shared property to all instances.
What is the output of this code?
```javascript
function Dog(name) { this.name = name; }
let d = new Dog('Buddy');
console.log(d.name);
```
Analysis & Theory
`d` is an instance of `Dog`, and `d.name` equals `'Buddy'`.