Skip to content

Commit 0070259

Browse files
committed
callback intro
1 parent 3bb54bb commit 0070259

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/index.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@ const app = document.getElementById('app');
44
app.innerHTML = '<h1>JavaScript Basics</h1>';
55

66
// --------------------------------------------------
7-
// Function IIFE immediatley Invoked Function Expression
7+
// Functions and Callbacks
88
// --------------------------------------------------
99

10-
// expression by wrapping in parens and calling it immediately
11-
// like JQuery
12-
const carPartId = (function (id) {
13-
const theId = `CAR_PART_${id}`;
14-
return function(name) {
15-
return `${theId}_${name}`;
16-
};
17-
})('XAT5461');
10+
function carPartId(name, fn) {
11+
const theId = `CAR_PART_XAT5461`;
12+
fn(theId);
13+
}
1814

19-
// Encapsulation of ID
20-
console.log(carPartId('LEFT_DOOR'));
21-
console.log(carPartId('RIGHT_DOOR'));
22-
console.log(carPartId('WINDSCREEN'));
15+
carPartId('LEFT_DOOR', function(id) {
16+
console.log(`Car Part Id: ${id}`);
17+
});
18+
19+
/**
20+
* Here the carPartId calls the function
21+
* The id is created
22+
* the passed fn is then called with the id
23+
* the original caller is then able to log the ID
24+
* CALLBACK
25+
*/

0 commit comments

Comments
 (0)