Factory functions & module pattern Flashcards
What are factory functions?
Functions that create and return objects and are not constructors/classes.
What is the only way to access functions etc. in the factory function?
By returning them
What is the concept of closure? (e.g function draw() inside factory function)
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
What are private variables and functions?
They are variables/functions which can only be accessed in the scope they were initialized in and not outside of it
How do you inherit one function (or sth) from objects with factories? (syntax)
const {sayName} = Person(name)
(Person(name) is method from another object)
How do you inherit everything with factories? (syntax)
return Object.assign(nr1, nr2, nr3)
What does IIFE stand for?
Immediately Invoked Function Expression
Explain IIFE concept.
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)
What does _ mean before some methods and properties?
It’s there to prefix a private method/property since JS does not have a private keyword for that
Why the concept of private functions is useful?
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.