JS ninja Flashcards

1
Q

what are the steps of the page building phases?

A
  1. Parsing the html and building the DOM

2. Executing JS code

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

What is the DOM?

A

It’s a structured representation of the HTML page in which every HTML element is represented as a node

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

what happens when the browser encounters the script tag while constructing the DOM?

A

the browser pauses the DOM construction from HTML code and starts executing JavaScript code

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

which is the primary global object that the browser exposes to the JS engine?

A

The primary global object that the browser exposes to the JavaScript engine is the window object

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

how does the browser keeps track of all the events that have occurred?

A

the browser uses an event queue

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

Describe the event handling loop

A
  1. check the head’s queue for pending event handlers to be executed
  2. if the is one, goto step 3, else goto step 1
  3. executed the event handler, goto step 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

which are the 2 phases of the execution of a client app?

A
  1. page building

2. event handling

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

how many event can be processed at a time?

A

only one, JS in single threaded by default

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

In what order are events from the event queue processed?

A

In the same order in which they were triggered: the queue follows a FIFO logic
first in first out

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

what are the 3 most important ways that JS provides for the definition of a function

A
  1. function declarations and function expressions
  2. arrow functions
    3 functions generators
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are rest parameters?

A

Rest parameters enable us to reference the remaining arguments that don’t have matching parameter names.

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

which are the implicit function parameters?

A

The implicit function parameters are ‘this’ and ‘arguments’

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

What is the ‘this’ parameter?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How many ways are there for invoking a function in JS? List them and briefly describe them

A

There are 4 way:

  1. The simple form foo()
  2. as a method ninja.foo()
  3. as a constructor new Foo()
  4. via the apply or call methods foo.apply(ninja) or foo.call(ninja)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the value of the ‘this’ object when invoking a function in the first simplest form => foo()

A
  • In the nonstrict mode it will the global context, the window object
  • in strict mode it will be undefined
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What’s the difference between function constructors and constructor functions?

A

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.

17
Q

what steps are triggered when using the keyword ‘new’ to call a function?

A
  1. A new empty object is created.
  2. This object is passed to the constructor as the this parameter, and thus becomes the constructor’s function context.
  3. The newly constructed object is returned as the new operator’s value (with an exception that we’ll get to in short order).
18
Q

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;
}
A
  1. 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.
  2. If, however, a nonobject is returned from the constructor, the returned value is ignored, and the newly created object is returned.
19
Q

does arrow functions has the “this” value?

A

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.

20
Q

explain the bind method

A

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.

21
Q

give the definition of scope

A

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.

22
Q

what’s the cost of a closure ?

A

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.

23
Q

what the difference between function context and execution context?

A

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.

24
Q

what is a lexical environment?

A

A lexical environment is an internal JavaScript engine construct used to keep track of the mapping from identifiers to specific variables

25
Q

how does js keeps tracks of an outer environment from a lexical environment?

A

JavaScript does this by taking advantage of functions as first-class objects.

Whenever a function is created, a reference to the lexical environment in which the function was created is stored in an internal (meaning that you can’t access or manipulate it directly) property named [[Environment]]; double brackets is the notation that we’ll use to mark these internal properties.

26
Q

describe js executions phases

A

The execution of JavaScript code occurs in two phases.

  1. The first phase is activated whenever a new lexical environment is created. In this phase, the code isn’t executed, but the JavaScript engine visits and registers all declared variables and functions within the current lexical environment.
  2. The second phase is the JS execution
27
Q

what is a closure?

A

it’s a mechanism that allows a function to access all identifiers that are in scope when the function itself is created

28
Q

what are generetors?

A

Generators are a special type of function. Whereas a standard function produces at most a single value while running its code from start to finish, generators produce multiple values, on a per request basis, while suspending their execution between these requests.

29
Q

what is a promise?

A

Promises, on the other hand, are a new, built-in type of object that help you work with asynchronous code. A promise is a placeholder for a value that we don’t have yet but will at some later point. They’re especially good for working with multiple asynchronous steps.

30
Q

what new syntax is introduced for generators?

A

the asterisk after the “function” keyword must be used. That enables us to the keyword yield inside the body of the generator, to produce individual values

31
Q

what kind of loop is used to consume the values of a generator function?

A

the “for-of” loop:
for(let weapon of WeaponGenerator()) {
assert(weapon, weapon);
}

32
Q

what does a generator function returns?

A

no return statement is used inside a generator, unlike standard functions generators implicitly creates and return an object called “iterator”

33
Q

describe the iterator object

A

it’s used to control the provided values from a generator.
It has a next() method: As a response to that call, the generator executes its code until it reaches a yield keyword that produces an intermediary result (one item in the generated sequence of items), and returns a new object that encapsulates that result and tells us whether its work is done.

34
Q

how would implement a “for-of”?

A
using a while:
let item;
while(!(item = iterator.next()).done) {
...
}
35
Q

what is this syntax “yield* foo()” used for

A

it’s like calling a function from a function but for generator. All calls to the original generator are rerouted to the foo() generator as long as he has values