What Is A Closure
What Is A Closure
When a function is defined inside another function, it forms a closure. The inner
function has access to the variables and parameters of the outer function, even
after the outer function has finished executing. This means that the inner function
"closes over" the variables of the outer function, hence the name "closure".
Scope
In JavaScript, scope refers to the set of variables, functions, and objects that
are accessible in a particular part of the code during runtime.
Global scope: Variables and functions declared outside of any function or block are
considered to be in the global scope. These variables and functions are accessible
from anywhere in the code.
Local scope: Variables and functions declared inside a function or block are
considered to be in the local scope. These variables and functions are accessible
only within that function or block.
Call back
In JavaScript, a callback function is a function that is passed as an argument to
another function and is executed by that function when a certain event or condition
occurs.
call,apply,bind
call: The call method allows you to call a function with a specified this value and
arguments provided individually as arguments. The first argument of the call method
is the this value, and the remaining arguments are the arguments to be passed to
the function.
apply: The apply method is similar to call, but it accepts arguments as an array
instead of individually. The first argument of the apply method is the this value,
and the second argument is an array of arguments to be passed to the function.
bind: The bind method returns a new function with the same function body as the
original function but with a bound this value. The first argument of the bind
method is the this value, and the remaining arguments are the arguments to be
passed to the function. The bound function can be called later with any additional
arguments.
The JavaScript engine is responsible for parsing and executing your code. The most
common JavaScript engine is V8, which is used in Google Chrome and Node.js. Other
popular engines include SpiderMonkey (used in Firefox) and JavaScriptCore (used in
Safari).
The event loop is a mechanism that allows JavaScript to handle asynchronous events
such as user input, network requests, and timers. The event loop constantly checks
for new events and executes their associated callbacks.
The call stack is a data structure that keeps track of the execution context of
your code. Whenever a function is called, a new frame is added to the top of the
stack, and when the function returns, the frame is removed. This allows JavaScript
to maintain the correct execution order of your code.
The heap is a region of memory where objects are stored. When you create a new
object in JavaScript, it is allocated on the heap.
When you run JavaScript code, it goes through several stages. First, the code is
parsed and compiled into bytecode by the JavaScript engine. Then, the bytecode is
executed, and any errors are caught and handled. Finally, the engine runs the
garbage collector to free up memory that is no longer being used by your code.
Overall, the JavaScript runtime process is a complex and dynamic environment that
handles many different types of events and operations. Understanding how it works
can help you write more efficient and reliable code.
In JavaScript, the global execution context is the default execution context that
is created when your code is first run. It is the outermost execution context and
contains all the global variables and functions that are available in your code.
The global execution context is created in two phases: the creation phase and the
execution phase.
In the creation phase, the JavaScript engine sets up the global scope, creates the
global object (which in a web browser is the window object), and creates a variable
called this that refers to the global object. It also scans through your code and
creates a list of all the functions and variables that are defined in the global
scope, known as the variable object.
In the execution phase, the JavaScript engine starts executing your code line by
line. It creates a new execution context for each function that is called and adds
it to the call stack. When a function returns, its execution context is removed
from the call stack and control is returned to the calling function.
The global execution context remains on the call stack until your code finishes
executing. Any variables or functions that are defined in the global scope are
available throughout your code and can be accessed from any other execution
context.