Factory functions & module pattern Flashcards

1
Q

What are factory functions?

A

Functions that create and return objects and are not constructors/classes.

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

What is the only way to access functions etc. in the factory function?

A

By returning them

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

What is the concept of closure? (e.g function draw() inside factory function)

A

Even though draw() was returned in the factory function and it can be accessed globally, it still has retained its scope. So, draw() has access to everything in the factory function even if it gets called outside the function

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

What are private variables and functions?

A

They are variables/functions which can only be accessed in the scope they were initialized in and not outside of it

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

How do you inherit one function (or sth) from objects with factories? (syntax)

A

const {sayName} = Person(name)
(Person(name) is method from another object)

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

How do you inherit everything with factories? (syntax)

A

return Object.assign(nr1, nr2, nr3)

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

What does IIFE stand for?

A

Immediately Invoked Function Expression

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

Explain IIFE concept.

A

Write a function, wrap it in parentheses and then immediately call it by adding empty parentheses next to it (Alex used that in my make-your-game)

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

What does _ mean before some methods and properties?

A

It’s there to prefix a private method/property since JS does not have a private keyword for that

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

Why the concept of private functions is useful?

A

In your code there are probably several supporting functions that do not be needed to use globally. Tucking them away makes it easier to test code, refactor etc.

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