Javascript Language Characteristics Flashcards
What is a closure and how is it created?
A closure is when a function remembers its lexical scope even when it is executed outside of its lexical scope.
The module pattern requires you to enclose an inner function with an outer function and return a reference to the inner function from inside of that larger function.
It is created when you call the inner function outside of its scope.
How long does a closure stay around?
As long as there is a reference to that inner function
When should you use a WHILE loop?
When you do not know ahead of time how many times you need to loop. For example, you do not know the length of the items in a collection.
Lexical Scope in Javascript
Can refer to a variable by name without getting access errors
Global Scope in Javascript
Outer most scope and can be accessed anywhere else in the program
var newSaga = function( ) { foil = aFoil ( ) }
What will the following code above do with the variable foil?
JS interpreter will automatically create a variable for foil in the global scope
var newSaga = function ( ) { if ( checkSomething( ) ) { var foil = aFoil( ) } }
Does the code block between the curly braces after the If statement create a new scope?
No, it does not. Only the curly brackets after a function declaration creates a new scope.
var newSaga = function( ) { var foil = aFoil ( ) }
What will the following code above do with the variable foil?
It will declare a variable called foil inside of the lexical scope for newSaga.
What is the difference between the loose equality ==
and strict equality ===
Strict Equality === compares the values and the types
Loose Equality == compares the values
What is the difference between the bracket notation and dot notation when used with Objects in JS?
Bracket notation allows you to use variables as properties.
Dot notation explicitly uses that name as a property in an object.
Synchronous
Statement executes one after the other, on a single, unbroken timeline
Asynchronous
- Happens at an unknown or unpredictable time
- Callbacks are the default JS technique to handle Async code BUT this could lead to the pyramid of Doom / callback hell! – a way to avoid callback hell is to use promises
see: https://classroom.udacity.com/courses/ud898/lessons/5972243496/concepts/60459332750923
- Events, Network, Threads are example of asychronous behavior in JS.
Promises
4 States:
- Resolved – completed task
- Rejected – error
- Settled – Fulfilled or rejected
- Pending – still waiting
Can only settle once
When to use promises?
- working with data from AJAX requests
- web workers (runs on separate threads Async)
What is a paradigm?
Design patterns used in programming:
- Object Oriented
- Functional
Explain Event Bubbling
Events rise up like bubbles through the DOM tree. Bubbling allows us to listen for events on ancestor elements.