Java Script - The Advanced Concepts Flashcards
Memorize the advanced concepts of JS as presented in Andrei Neagoie's course with the same name.
What does it mean that JavaScript is a non-blocking language?
It means that while it’s a single-threaded language, it can have asynchronous behavior (so that a asynchronous line doesn’t block the rest of the code).
Is JavaScript compiled or interpreted?
It’s compiled through JIT compilers that try to combine aspects from compiled and interpreted languages.
How does the JIT compiler works?
It checks for possible code optimizations and compile them on the fly
Which expressions are hard to optimize ?
eval, arguments, for…in, with and delete
What should you do with objects to optimize hidden classes ?
Add all properties in the same order for similar objects
How does hidden classes work?
Every object has a hidden class. Once a new property is added, a new hidden class is created and a transition between them is stablished.
How is the garbage collection done in JS ?
Mark and Sweep (mark all objects that are referenced in memory, sweep the ones that weren’t marked before).
Which are some of the most common causes of memory leaks in JS?
Use of global variables, not removing obsolete event listeners.
How can you access the methods from the web API?
With the “window” command.
When is the callback queue executed ?
When the callstack is empty
What is the predominant scope used in JS?
Static (lexical) scope
When does hoisting happen ?
Every time a new execution context is created
Which keywords are hoisted?
Functions are fully hoisted; vars are partially hoisted.
What’s the most efficient way to iterate through objects?
Use Object.keys (obj) to get the keys in a separate array and iterating through it.
Why are the “eval” and “with” keywords hard to optimize?
Because they distort how the variable scope works.
How can you check the scope of a function ?
Through the [[scope]] block.
What happens when you create a variable without a “var”, “let” or “const” identifier?
It is created on the global scope.
Is the “var” keyword local, function or global scoped ?
function
Is the “let” keyword local, function or global scoped ?
local
What’s the syntax of IIFEs?
(function(…) {…} ) () ;
Are IIFEs hoisted? Why?
No, because they don’t start with the “function” keyword.
What’s the biggest advatage of IIFEs?
It helps keeping the global namespace clean by encapsulating all variables and functions inside of a single globally scoped variable.
Is the “this” keyword lexically or dynamically scoped?
Dynamically
Is it possible to change the scope of the “this” keyword ? How?
You change the scope of the “this” keyword to lexical scope when you use it inside of an arrow function.
” ‘function (x) {…}’ is equivalent to ‘(x) => {…}’ “. True or false statement?
False. On the first one, “this” will have dynamic scope. In the second, it has lexical scope. The second one also doesn’t have access to the “arguments” keyword.
What method is called when you run a function ?
The call ( ) method from the same function you are trying to run.
What are the two most commonly used ways of sharing the same method between different objects ?
Using the “this” keyword inside the method or through method.apply( ).
What’s the meaning of the first parameter of the call () method?
The object at which the function should be called upon. If null is passed, it will be called upon the window object.
What does “function.bind()” does ?
It binds the “this” keyword inside the function to the object passed as a parameter.
“Functions in JS are just objects”. True or False ?
True
Is JS strongly or weakly typed ? What does it mean?
Weakly. It means that the language is more lenient when dealing with operations with different types
Is JS statically or dynamically typed? What does it mean?
Dynamically. It means the type of the variables are defined on execution instead of compilation.
Functions are which kind of object?
Callable objects
Why are closures needed in JS?
They are needed to guarantee that any functions declared inside another function can have access to their variables even if the parent function was already executed and is not on memory anymore.
What are the biggest advantages of using closures ?
Time and memory efficiency. They also permit the creation of private variables in a OOP context.
What kind of inheritance is implemented in JS?
Prototypal Inheritance
Which type in JS has the “prototype” property?
Functions
“The reccomended way of inheriting a prototype is by changing the __proto__ property to the desired parent object”. True or False?
False. The preferred method is through “Object.create(parent)”
“When checking for a property in an object, JS will start with the top of the prototype chain and follow down until the object”. True or False?
False. It’s the opposite direction.
What’s the biggest advantage of prototypal inheritance over classic inheritance?
On classical inheritance, only classes can be inherited from classes. Prototypes can be inherited by other prototypes and objects.
What’s the main use of prototypes in JS?
Simulating OOP.
How can you create objects (in OOP sense) in JS?
Through “Object.create(constructor)”
How can you add properties to constructors in JS?
By assigning them to the function prototype
Which operator is used to create OOP objects in JS?
The “new” operator.
“When you create two different objects from the same prototype, their functions will be pointing to the same memory address”. True or False?
True if the function was added to the prototype, false if it was created inside the constructor itself.
“The only way to add variables to constructors is through the ‘this’ keyword”. True or False?
True
What’s the difference between imperative and declarative programming?
Imperative is defining what to do and how to do. Declarative is about telling what to do and what’s the expected result.
Which Functional Programming principle does memoization use?
Memoization works through caching the result of previous calls to make future calls faster. This requires the idempotence principle to work (the idea that a function should return the same output for the same input).
How can you create private variables in JavaScript?
By using closures to encapsulate them.
What does currying means?
It means taking a function that receives n arguments and turning it into n functions that receive one argument and return a function for the next.
What’s the difference between currying and partial application?
Currying is a partial application one argument at a time. Partial application can also be used to create a function that receives only the first argument and return a function that receives all the others simultaneously.
What’s the meaning of arity?
The amount of parameters a function receives
How can you improve the arity of a function?
By converting it into a function that applies the argument only partially.
When should Functional Programming be preferred to Object Oriented Programming?
When you are dealing with massive amounts of data.
When should Object Oriented Programming be preferred to Functional Programming?
When there are many objects that could share the same methods
What does it mean to compose functions?
It means to apply a function on the result of a function.
What’s the difference between compose and pipe?
Composing two functions f and g means executing g and applying f to the result. Piping them means executing f and applying g to the result.
“JavaScript is a synchronous, single-threaded, non-blocking language”. True or False?
True
Which are the possible states for promises?
Fulfilled, Rejected or Pending
What does it mean that a promise was settled?
It means that it was completed, either fulfilled or rejected.
How is the standard way of creating promises?
const p = new Promise ( (resolve,reject) => {if (…) resolve() else reject()} );
When does the execution of a promise begin?
Right after it’s instantiation
What happens when you use “promise.then()”?
It will execute the function inside the “then()” part after it’s finished executing; the parameters passed to the function will be the return of the promise.
What’s the type of the return of the resolve() and reject() functions?
Promises
What happens when a promise gets rejected inside a chain?
The following “then()” promises won’t execute and the code will look if there’s any “.catch()” to handle the reject scenario
What’s the difference between “Promise.all()” and “Promise.allSettled()”?
Promise.all() will only execute the following “then()” functions if all the promises inside are fulfilled. Promise.allSettled() will execute after all the promises finish execution (doesn’t matter how many, if any, gets fulfilled).
“Async / Await is just syntatic sugar for promises”. True or False?
True
“The finally block is also available with promises to clear up things whether they get resolved or rejected”. True or False?
True
How can you iterate through a promise array?
With “for await (… of …)”.
When are promises executed (in relation to the synchronous JS code)?
After the code has finished executing (but before it has returned) and the stack is empty.
Do promises use the job queue or the callback queue?
Job Queue
What’s the difference between the “job queue” and the “callback queue”? Which (if any) has priority?
The “job queue” (also known as micro-task queue) is for more relevant (and usually faster) operations wrapped around promises. It has priority over the callback queue (that’s used with the web API).
How are the three ways you can execute a different group of promises?
Parallel (all of them running at the same time, the code returns when all of them finish executing), sequential (one after the other, code returns when the last one finishes) and race (all at the same time, code returns the result of the first one to finish).
How are the asynchronous functions handled underneath the hood?
With service workers that work on different threads.