mode 5 JS III Flashcards
What is a scope?
- the memory space where a variable exists
- it has NOTHING to do with permissions, nothing to do with access levels.
What scopes does JavaScript support?
- global scope: the variables/entities can be accessed by everything
- function scope: only accessible withing the specific function it is declared
- block scope: only accessible within the block it is declared
Hww do I declare a variable in JS?
- var only enforce the “global” and “function” scopes
- let enforces all 3 scopes
- const same as “let” BUT it makes the value immutable
Scope access
I CAN access my global variable from the global scope,
I CAN access my global variable from the function scope,
I CAN access my function variable from the function scope,
I CAN access my global variable from the block scope,
I CAN access my function variable from the block scope,
I CAN access my block variable from the block scope,
I CAN access randomVar from the block scope
I CAN access my block variable from the function scope, if the block variable was declared using "var" // block scope only functions appropriately when we use "let" to declare variables
What is Hoisting?
Essentially, JS is a two pass translator. On the first pass through of a file, JS will find all global declarations and create those variables/functions. On the second pass through, JS will perform your program’s logic (like assignment, for-loops, method calls, etc).
How do we work with VARIABLES WITH THE SAME NAME (but different scopes)
- variables in a higherscope will be shadowed, meaning that they are more difficult to access.
- using the variable name will access the “most immediate” scope’s version of the variable
Using VARIABLES WITH SAME NAME AND THE SAME SCOPE
there is a window object inside of javascript’s “global scope”. Functions will be created inside of
this window object, using their names as their variableNames in the window object. SO, when you
create a second function with the same name, you’re essentially just reassigning the window
object’s variable
DOES OVERLOADING EXIST IN JS?
no, overloading doesn’t exist in JS like you’d expect
What is an anonymous function?
ANONYMOUS FUNCTION: a function without a name
What is a callback function?
CALLBACK FUNCTION
-it’s a function that is passed as an argument to another function to be run at a later point in time.
Basically, using callback functions are used to chain functions and control the order in which they are called
SELF-INVOKING FUNCTIONS
What is an IIFE?
It is similar to initializer block
-Immediately Invoked Function Expression (aka Self-invoking function)
An IIFE is a function that calls itself.
What is a NESTED FUNCTION
A function inside another function
What is CLOSURE in JS?
- Closure are a way to create encapsulation in JS.
- A closure is a self invoking anonymous function that returns an inner function
What is Arrow Notation?
ARROW NOTATION (function shorthand notation)
(a,b) => a+b; //this will return a+b immediately (automatic/implicit return value) OR (a,b) => {a+b; console.log(a+b);} //no implicit return value OR a => {console.log(a)} //no implict return value OR () => {console.log("stuff")} //no implicit return value
What is the DOM?
- Document Object Model
- The DOM is a virtual representation of the HTML page