Building Objects Flashcards

1
Q

What is new and what does it do?

A

new is actually an operator.

When we call new, immediately an empty object is created and then it invokes the function next to it. A very important point is that the this keyword now belongs to that empty object and not the function.

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

What is a function constructor?

A

A normal function that’s used to construct objects.

The this variable points to a new empty object, and that object is returned from the function automatically.

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

What’s the ideal way to set up properties and methods on a function constructor?

A

Properties should be set up in the constructor itself, but methods should be set up on the prototype. Properties will probably be unique to each object, so it makes sense to store them within each object. However, methods are most likely going to be the same for every new object. As more and more objects are constructed, this approach willl save memory space because they’ll all be pointing to just one prototype method instead of possibly thousands of copies of the same method.

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

How do prototypes work in function constructors?

A

The Function object has a special prototype property, whose only purpose is to be used on function constructors to provide a global prototype property to all objects created by that constructor - it isn’t used for anything else by the Function object.

It’s a confusing name because you might think you’re accessing the prototype of this object, like by using __proto__. But, the prototype property of the function is not the prototype of the function - it is the prototype of any objects created if you’re using the function as a function constructor.

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

What happens if you forget the new operator when invoking a function constructor?

A

It will return undefined. Since a function constructor is still just a function and the JS engine doesn’t magically know your intent, it will just execute that function as it is. Properly written function constructors don’t have return statements in their code(because they auto-return the object). So, when a function constructor is invoked without the new keyword, it has nothing to return.

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

Why do we capitalize the names of function constructors?

A

It’s helpful to differentiate them, especially because of how critical the new keyword is. If you’re getting a bunch of errors and you notice a capitalized function call without a new, then you know that’s probably the source of your errors. Linting programs will catch this for you, but the JS engine won’t, so it’s helpful to use this capitalization approach.

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

What is the difference between:

3 and var a = new Number(3);

“jack” and var jack = new String(“jack”);

A

Although these look like the same thing, the left side is a primitive and the right side is actually a special object with the primitive value boxed inside of it, along with all the special methods that come from the prototype of that object. Each of them are actually objects made by Javascript’s built-in function constructors - Object(), String(), Number(), Boolean(), Array(), RegExp(), Function() and Date().

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

Given:

var a = 3;

var b = new Number(3);

What will these return and why?

a == b

a === b

A

a == b returns true

a === b returns false

The == operator will coerce a and b to the same type and then return true

The === operator will see that a is a primitive and b is an object created by a function constructor and return false.

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

Why is using for in loop avoided on arrays in JS?

A

Because arrays are actually objects and their items are added properties. Looping through the properties may actually include other properties that have been added to the prototype. It’s safe to a standard for loop.

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