forked from smartherd/DartTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
|
||
// Objective | ||
// 1. Closures | ||
|
||
|
||
void main() { | ||
|
||
// Definition 1: | ||
// A closure is a function that has access to the parent scope, even after the scope has closed. | ||
|
||
String message = "Dar is good"; | ||
|
||
Function showMessage = () { | ||
message = "Dart is awesome"; | ||
print(message); | ||
}; | ||
|
||
showMessage(); | ||
|
||
|
||
// Definition 2: | ||
// A closure is a function object that has access to variables in its lexical scope, | ||
// even when the function is used outside of its original scope. | ||
|
||
Function talk = () { | ||
|
||
String msg = "Hi"; | ||
|
||
Function say = () { | ||
msg = "Hello"; | ||
print(msg); | ||
}; | ||
|
||
return say; | ||
}; | ||
|
||
Function speak = talk(); | ||
|
||
speak(); // talk() // say() // print(msg) // "Hello" | ||
} |