Skip to content

Commit 95c683b

Browse files
author
ashfaq
committed
creating other function with closure
1 parent 180a7e5 commit 95c683b

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

closureCallback.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function sayHiLater(){
2+
var greeting = "Hi!";
3+
setTimeout(function(){console.log(greeting)}, 2000)
4+
}
5+
sayHiLater()
6+
7+
8+
9+
10+
function tellMeWhenDone(callback){
11+
var a = 100;
12+
callback();
13+
}
14+
15+
16+
tellMeWhenDone(function(){console.log("I am Done")})
17+
18+
tellMeWhenDone(function(){alert("I am Done")})

functionFactory.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function greet(language) {
2+
return function (firstName, lastName) {
3+
if (language === 'en') {
4+
console.log(`Hello ${firstName} ${lastName}`)
5+
}
6+
if (language === 'es') {
7+
console.log(`Hola ${firstName} ${lastName}`)
8+
}
9+
}
10+
}
11+
12+
var englishGreeting = greet('en');
13+
var spanishGreeting = greet('es');
14+
// value of language will be available in the closure;
15+
16+
englishGreeting('Charles', 'charli');
17+
spanishGreeting('pablo', 'paul');

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
<meta charset="utf-8"/>
77

88
<!-- <script src="collection.js"></script> -->
9-
<script src="closures.js"></script>
9+
<script src="closureCallback.js"></script>
1010
</body>
1111
</html>

0 commit comments

Comments
 (0)