September Flashcards

1
Q

thread of execution

A

Transcript from the “Thread of Execution” Lesson

JavaScript is not that fancy, it does two things.

One, it goes through our code, line by line. It goes through it line by line, and does each of the line of code, they call this the thread of execution. It threads its way down and executes, that’s a fancy word for doing the code, as it goes, that’s feature one of JavaScript.

Feature two of JavaScript is the stuff that it encounters, if it’s saying save something, like a variable, or a constant, or even some code itself, known as a function. Well, it saves that stuff in the computer’s memory, to come back to later on and use.

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

global memory

A

global variable environment

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

function expression vs

anonymous vs named

why have function expressions?

A

https://www.youtube.com/watch?v=gjLn95skIKE

  • anonymous f assigned to a variable, but it could be a named f
  • f declaration is hoisted, meaning you can call that f before it’s defined.
  • in f expression the var itself will be hoisted, but not the f
  • evaluated result stored on variable, defaults to undefined while it’s running the f
  • so if you print the var before it’s defined, it will be undefined. It won’t through an error
  • but if you try to run it it will throw a type error, it does not yet know it has been assigned to a function
  • with f expressions you can use fs as first class fs, because you can assign function to a var, then pass in another function when calling that var.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

local execution context

when is it created and destroyed?

how is it different from global?

A

function level execution context

https://www.youtube.com/watch?v=Nt-qa_LlUH0

created when function is invoked/executed/called.

the only difference of local vs global, is that local doesn’t create global object, instead it creates an arguments object
arguments is keyword in js that’s an array like object,
these are set the value in the creation phase

every time a function is invoked a new execution context is created and added on call stack, when both phases creation and execution are finished, it’s popped off the stack

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

parameter vs argument

when are they defined?

A

Parameters are variables listed as a part of the function definition.

Arguments are values passed to the function when it is invoked.

Why should we bother about this minute difference?

Well for starters, JavaScript does not throw an error if the number of arguments passed during a function invocation are different than the number of parameters listed during function definition. This should make it clear that parameters and arguments should be treated as two different entities.

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

invoke

A

= execute = call

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

call stack

A

tracking execution context

Execution context is a concept in the language spec that—in layman’s terms—roughly equates to the ‘environment’ a function executes in; that is, variable scope (and the scope chain, variables in closures from outer scopes), function arguments, and the value of the this object.

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

why do we need higher order functions

whats a callback

whats a higher order function

benefits

A

in js functions are first class objects - they have all the properties of objects, so we can store and pass them around as arguments, and they can be returned as a return value
higher order function takes in the callback, the function we pass in
1. simplify code and make it dry
2.make us run asynchronous code
3.making function reusable
4.composable

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

difference between function and object

A
  • the only difference is function as an object can be invoked, but object cannot
    *
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

global execution context, hoisting

A

https://www.youtube.com/watch?v=Nt-qa_LlUH0

each execution context has 2 phases: creation and execution

if you ran empty code first line of business for engine is to create 2 properties:
1. window : global object
2. this: window
then if we have code
3. set up memory space for var and f
we set var to undefined
and f declaration is put entirely in the memory

now it’s time for the execution phase:
assign values to var

the stage when var declarations are assigned default value undefined in the creation phase is called hoisting

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

What is scope chain?

A

14:15 https://www.youtube.com/watch?v=Nt-qa_LlUH0

if the engine can’t find the var in the local execution context it will look into the parent for that var, this process of lookup will continue until the global execution context and if it’s not there it’ll throw a reference error because it doesn’t exist anywhere in the scope chain or execution context chain.

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

closure scope

A
f makeAdder(x){
return f inner(y){
return x + y
}
}
var add5 = makeAdder(5)
count += add5(2)

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.

When inner gets run, it creates something that you can think of as a “COVE” (closed off variable environement), so before JS runs that code, it takes a look to see if there are any variables associated with that code in the current scope. If there are, it created a closure, basically a backpack, where those are stored specifically for that function/object. As for the stack order, it’d be outer, then inside outer we put inner, inner runs and is popped off the stack, outer finishes running and is popped off the stack.

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

type error vs reference error

A

when you have a function expression and console.log the var before it’s defined it’ll be undefined, but you won’t get reference error, which occurs when var is not in memory. It will give a type error if you call it though, because it doesn’t know yet that it’ll be assigned a function in the execution state, so it thinks it’s not a valid operation on this type.

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

this keyword

call, apply, bind

A

https: //www.youtube.com/watch?v=gvicrj31JOM
https: //codewithmosh.com/courses/324741/lectures/5142769

this is referencing the object that is executing the current function
method -> obj
function -> global window object
callback function -> global window object

solutions:
forEach method can pass a second parameter referencing the object, which is this

in a method: const that = this; you can use that. in the callback and it will reference the original value of that instead of the global object
don't use this method

function.call({}, 1, 2) put object you want to reference
first argument is the object, the other 2 are the parameters passed in function
function.apply({}, [1, 2])
function.bind({})() permanently binds object to the function

arrow function, inherits the this value from the containing function

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