JS ninja Flashcards
what are the steps of the page building phases?
- Parsing the html and building the DOM
2. Executing JS code
What is the DOM?
It’s a structured representation of the HTML page in which every HTML element is represented as a node
what happens when the browser encounters the script tag while constructing the DOM?
the browser pauses the DOM construction from HTML code and starts executing JavaScript code
which is the primary global object that the browser exposes to the JS engine?
The primary global object that the browser exposes to the JavaScript engine is the window object
how does the browser keeps track of all the events that have occurred?
the browser uses an event queue
Describe the event handling loop
- check the head’s queue for pending event handlers to be executed
- if the is one, goto step 3, else goto step 1
- executed the event handler, goto step 1
which are the 2 phases of the execution of a client app?
- page building
2. event handling
how many event can be processed at a time?
only one, JS in single threaded by default
In what order are events from the event queue processed?
In the same order in which they were triggered: the queue follows a FIFO logic
first in first out
what are the 3 most important ways that JS provides for the definition of a function
- function declarations and function expressions
- arrow functions
3 functions generators
What are rest parameters?
Rest parameters enable us to reference the remaining arguments that don’t have matching parameter names.
which are the implicit function parameters?
The implicit function parameters are ‘this’ and ‘arguments’
What is the ‘this’ parameter?
The this parameter refers to an object that’s associated with the function invocation. For this reason, it’s often termed the function context.
How many ways are there for invoking a function in JS? List them and briefly describe them
There are 4 way:
- The simple form foo()
- as a method ninja.foo()
- as a constructor new Foo()
- via the apply or call methods foo.apply(ninja) or foo.call(ninja)
What is the value of the ‘this’ object when invoking a function in the first simplest form => foo()
- In the nonstrict mode it will the global context, the window object
- in strict mode it will be undefined
What’s the difference between function constructors and constructor functions?
A function constructor enables us to create functions from dynamically created strings. On the other hand, constructor functions, are functions that we use to create and initialize object instances.
what steps are triggered when using the keyword ‘new’ to call a function?
- A new empty object is created.
- This object is passed to the constructor as the this parameter, and thus becomes the constructor’s function context.
- The newly constructed object is returned as the new operator’s value (with an exception that we’ll get to in short order).
what happens when a constructor returns a value? what if that value is an object, e.g:
const puppet = { rules: false; }
function Emperor() { this.rules = true; return puppet; }
- If the constructor returns an object, that object is returned as the value of the whole new expression, and the newly constructed object passed as this to the constructor is discarded.
- If, however, a nonobject is returned from the constructor, the returned value is ignored, and the newly created object is returned.
does arrow functions has the “this” value?
No, Arrow functions pick up the value of the this parameter at the moment of their creation, the value of this is taken from the context.
explain the bind method
It’s a method that every functions have and creates a new function. This function has the same body, but its context is always bound to a certain object, regardless of the way we invoke it.
give the definition of scope
a scope refers to the visibility of identifiers in certain parts of a program. A scope is a part of the program in which a certain name is bound to a certain variable.
what’s the cost of a closure ?
Each function that accesses information via a closure has a “ball and chain” attached to it, carrying this information around.
So although closures are incredibly useful, they aren’t free of overhead. All that information needs to be held in memory until it’s absolutely clear to the JavaScript engine that it’s no longer needed (and is safe to garbage-collect), or until the page unloads.
what the difference between function context and execution context?
function context is the object on which our function is invoked, which can be accessed through the this keyword. An execution context, although it has a similar name, is a completely different thing. It’s an internal JavaScript concept that the JavaScript engine uses to track the execution of our functions.
what is a lexical environment?
A lexical environment is an internal JavaScript engine construct used to keep track of the mapping from identifiers to specific variables