-
Environment created before any javacript code is executed, consists of:
-
global
window
orglobal
(Node.js) object -
this
object (equal towindow
orglobal
) -
arguments
object -
variable environment (all variable declarations)
-
-
write-time (detrmines variables solely based on their textual position in our code)
-
based on nesting of scope
-
the scope that can be determined statically at a write time
-
only created when you create a new function (
{}
) -
extended by parent lexical scope walking up scope chain until global execution context
-
can be accessed by
[[Scopes]]
-
run-time
-
based on callstack
-
the scope that can be determined dynamicaly at a runtime
-
function with reference to its lexical scope
-
in "normal" function when it is returned, variables declared inside it are garbage collected, in closures they are not
-
concept of closures is similar to classes with private methods
-
uses for data privacy, keep "secret" data inside a function and only allow reading it via access methods
function a() {
let grandpa = 'grandpa'
return function b() {
let father = 'father'
let random = 4327493274233379 // garbage collected!
return function c() {
let son = 'son'
return `${grandpa} > ${father} > ${son}`
}
}
}
-
Act of moving of
function
orvar
to the top of the scope.-
because javascript allocates space for them in a memory heap
-
during compilation phase
-
var
initialized withundefined
-
let
andconst
declarations remain uninitialized
-
-
Design pattern to avoid namespace collisions.
-
used before modules were introduced
-
E means Expression, declaration starts with
function() {}
-
you can pass a param to avoid scope chain lookup
-
-
The object that the property or function is property of.
-
refers to what is left to the dot
'.'
-
the value of
this
can be determined by answering the question "who called the function?"
-
- can be used for method borrowing, arguments are coma-separated
originalObject.method.call(targetObject, argument1, argument2, ...)
- can be used for method borrowing, arguments are in
Array
originalObject.method.apply(targetObject, [argument1, argument2, ... ])
-
returns new function unlinke
call
orapply
-
allows to store in function for future use
const method = originalObject.method.bind(targetObject, argument1, argument2, ...)