Prepare List
1 | How to hide element (visually hide) |
Really Good Resources
Web development
Javascript
this
keyword andscope
in JS- Inheritance in JS
- A resource hub
- Event delegation, closure, and debouncing
- (By a Amazon Manager)Preparing for a Front-End Web Development Interview at Top Tech Companies
- Have to know
Execution Context and Closure
Lexical Scope.
A closure is basically when an inner function has access to variables outside of its scope.
Q: Write a function that will loop through a list of integers and print the index of each element after a 3 second delay
Question: How to properly use closures in loops? Quick answer: Use a function factory.
https://stackoverflow.com/questions/3572480/please-explain-the-use-of-javascript-closures-in-loops
Key Points:
- A closure captures the variable itself.
- Using a simple closure in a loop will end up with all closures sharing the same variable and that variable will contain the last value assigned to it in the loop
- What we want is an instance of that variable or at least a simple reference to the variable instead of the variable itself. Fortunately javascript already has a mechanism for passing a reference (for objects) or value (for strings and numbers): function arguments! When a function is called in javascript the arguments to that function is passed by reference if it is an object or by value if it is a string or number. This is enough to break variable sharing in closures.
1 | for (var i=0; i<10; i++) { |
1 | const loopThrough = (n) => { |
Event Delegation and bubbling
Add the event listener to the parent element, when the event bubbles up to the <ul>
element, you check the event object’s target property to gain a reference tot he actual clicked node.
1 | <ul id="parent-list"> |
1 | // Get the element, add a click listener... |
Object prototypes, constructors and mixins.
Composition and high order functions.
Type Coersion
JavaScript type coercion explained
Asynchronous calls
DOM
- Select Node:
document.querySelector()
x - Traverse up-and-down:
Node.parentNode()
,Node.firstChild()
,Node.lastChild()
,Node.childNodes()
. - Traverse left and right:
Node.previousSibling()
,Node.nextSibling()
.
Create, copy, update nodes:
- Create new node:
document.createElement()
,document.createTextNode()
- Update text of node:
node.textCotent = ...
,node.innerHTML = ...
Insert node into DOM:
- Add node as the last child:
parentNode.appendChild()
- Insert node before a sibling node under parent node:
parentNode.insertBefore(newNode, nextSibling);
- Replacing an existing node:
parentNode.replaceChild(newNode, oldNode)
Remove node from DOM:
- Remove child node:
node.removeChild(childNode)
- Remove node:
node.remove()
Modify element attributes:
element.hasAttribute('href')
element.getAttribute('href')
element.removeAttribute('href')
element.setAttribute('href')
Modify element classes:
element.className
: Return a set of class nameselement.classList.add('newClass')
: Add one / more classeselement.classList.toggle('class')
: toggle classelement.classList.replace('old', 'new')
: replace old class with new classelement.classList.remove('active')
: remove class
Set style
div.style.height = '200%'