Skip to content

Commit

Permalink
Lexical Closures
Browse files Browse the repository at this point in the history
  • Loading branch information
smartherd committed Sep 1, 2018
1 parent 66348bd commit 94f766e
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions 30_lexical_closures.dart
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"
}

0 comments on commit 94f766e

Please sign in to comment.