-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
25 lines (20 loc) · 761 Bytes
/
app.js
File metadata and controls
25 lines (20 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// call - runs instantly, arguments - list of items
// apply - runs instantly, arguments - array of items
// bind - assign, use later, arguments - list of items
const btn = document.querySelector(".increment");
const counter = {
count: 0,
increment() {
this.count = this.count + 1;
console.log(this);
console.log(this.count);
},
};
// this will fail, since it points to btn
// btn.addEventListener("click", counter.increment);
// some edge cases like remove event listener needs a function reference not a function
// btn.addEventListener("click", counter.increment.bind(counter));
// this will work
const increment = counter.increment.bind(counter);
btn.addEventListener("click", increment);
btn.removeEventListener("click", increment);