constructors Flashcards

https://css-tricks.com/understanding-javascript-constructors/ https://content.pivotal.io/blog/javascript-constructors-prototypes-and-the-new-keyword

1
Q

Constructors are like regular functions, but we use them with the _______ keyword

A

new

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

There are two types of constructors

A
  1. built in

2. custom

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

It’s a convention to ____________ of constructors to distinguish them from regular functions

A

capitalize the name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
function Book() { 
  // unfinished code
} 

var myBook = new Book();

myBook instanceof Book //

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
function Book() { 
    // unfinished code
  } 

var myBook = new Book();

alert(myBook.constructor !== Book);

A

false

constructor points to Book

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
var s = new String("text");
s.constructor === String;      // 
"text".constructor === String; //
A

true

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
var a = new Array();
a.constructor === Array;       // true

[].__________ === Array; // true

A

constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Although the constructor property can be used to check the type of an instance, it is generally recommended to only use the instanceof operator for this purpose

TRUE / FALSE

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

to check the type of an instance use instanceof or constuctor

WHY?

A

instanceOf

because the constructor property might be overwritten. As a result it’s not a reliable method for checking the type of an instance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

A getter is expected to___________, while a setter receives the value being assigned to the property as an argument.

A

return a value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The JavaScript language has _______ built-in constructors

A

9

Object(), 
Array(), 
String(), 
Number(), 
Boolean(), 
Date(), 
Function(), 
Error()
RegExp()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Object literal notations are preferred to constructors

TRUE / FALSE

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

It’s important to remember to use the new keyword before all constructors. If you accidentally forget new, you will _______

A

be modifying the global object instead of the newly created object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

vehicle; // {}

var FuzzyBear = function FuzzyBear() { };
vehicle.constructor = FuzzyBear;

vehicle; // { constructor: function FuzzyBear() }
vehicle.constructor == FuzzyBear; //
vehicle instanceof FuzzyBear //
vehicle instanceof Vehicle //

A

true
false
true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly