Event Bubbling
My quest to learn event bubbling and the DOM event model :) // DOM event model // - bubbling // subscription ("Listening") // publishing ("trigger") //
by Eugen Sunic
JavaScript
class Node {
constructor(element) {
this.element = element;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.size = 0;
}
add(element) {
var node = new Node(element);
if (!this.head) {
this.head = node;
this.size++;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = node;
this.size++;
}
insertAt(element, index) {
const node = new Node(element);
if (index > this.size) {
return;
}
if (!this.head && index === 0) {
this.head = new Node(element);
++this.size;
return;
}
if (index === 0 && !!this.head) {
this.head.next = this.head;
this.head = new Node(element);
++this.size;
return;
}
let counter = 0;
let current = this.head;
let previous = null;
while (counter < index) {
previous = current;
current = current.next;
counter++;
}
previous.next = node;
node.next = current;
++this.size;
}
/* removeAt(index) {
let counter = 0;
const current = this.head;
const previous = null;
while (counter < index) {
previous = current;
current = current.next;
counter++;
}
previous.next = previous.next.next;
this.size--;
} */
removeLast() {
let current = this.head;
let previous = null;
while (current.next) {
previous = current;
current = current.next;
}
previous.next = null;
this.size--;
}
reverseList(head) {
var node = head;
var previous = null;
while (node) {
// save next or you lose it!!!
var save = node.next; // 8, 7
// reverse pointer
node.next = previous; //null, 9
// increment previous to current node
previous = node; //9 8
// increment node to next node or null at end of list
node = save; //
}
return previous;...