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.