Professional Documents
Culture Documents
Software Studio: Functions, Scope & Closures
Software Studio: Functions, Scope & Closures
studio
functions, scope & closures
Daniel Jackson
1
functions as values
2
making functions
> three()
> id(true)
true
> id(id)
> (id(id))(3)
3
functions are first class
> seq = function () {
> seq.c = 0
> seq()
> seq()
4
4
recursion
can you explain
› how a recursive definition works?
fact(i-1);}
24
5
a puzzle: repeated applications
6
evaluating functions
7
two phases
> (function (x) {return x + 1;}) (3)
creation
› function expression evaluated
application
› function body evaluated
evaluation order for applications
› first evaluate arguments, left to right
› then evaluate body
> log = function (s) {console.log(s + seq());}
a1
b2
c3
8
evaluating the body
> f()
> x = 2
> f()
9
two environments
when function is created
› keeps environment as a property
› called ‘function scope’
› uses this environment to evaluate body in
what about arguments?
› new environment (‘frame’) with bindings for args
› linked to function scope
10
body of function is
an object model evaluated in context
of function’s scope
_proto
context !
Activation Environment
?
!
creates
stmt _scope
scope of a function
! ? ? is context it was
_body ?
created in
Statement Function
Courtesy of Harold Abelson and Gerald Jay Sussman. Used with permission.
12
13
example 1
x;}
function () {return x;}
x;}
1
> x = 1
1
1
> f()
f()
1
> x = 2
f()
> f()
14
simulating example 1
x
var
(Var)
f = function () {return x;} stmt 1. f = function () {return x;} context bindings
(Env) (Binding)
(Statement) (Activation)
creates 21
_scope (Object)
value
2. x = 1
(Function)
(Activation)
3. f()
(Activation)
body
x;}
function (x) {return x;}
x;}
> x = 1
1
1
1
> y = 2
2
2
2
f(y)
> f(y)
16
simulating example 2
x
var (Var)
f = function (x) {return x;} stmt 1. f = function (x) {return x;} context bindings
(Env) (Binding)
(Statement) (Activation)
creates 1
_scope value (Object)
bindings
2. x = 1
(Function)
(Activation) y
var (Var)
(Binding)
3. y = 2
2
(Activation) value
(Object)
body _proto
> x = 1
> y = 2
> f(y)
17
2
example 3
>> xx == 11
11
>> ff == (function
(function (x)
(x) {return
{return function
function ()
() {return
{return x;};})
x;};}) (x)
(x)
function
function () () {return
{return x;}
x;}
>> f()
f()
>> xx == 22
22
>> f()
f()
18
simulating example 3 x
var (Var)
1. x = 1 context bindings
(Env) (Binding)
(Activation)
21
value (Object)
f = (function (x) {
return function () stmt 2. f = ...
{return x;}}) (x) (Activation)
(Statement)
_scope
(Function) > x = 1
3. x = 2 1
(Activation) > f = (function (x) {
body
return function ()
4. f()
(Activation)
{return x;};}) (x)
function () {return x;}
> x = 2
4.1 return x
(Activation) 2
> f() 19
1
example 4
(0)
> f = (function (x) {return function () {x += 1; return x;};}) (0)
f()
> f()
f()
> f()
what if we modify x?
› when f is applied, x is bound to 0 in new frame
20
simulating example 4
f = (function (x) {
return function () stmt 1. f = ... context
(Env)
{x += 1; return x;};}) (0) (Activation)
(Statement)
creates _scope
(Function)
2.1 return function ...
_proto
(Activation) x
var (Var)
bindings
creates (Env) (Binding)
01
value (Object)
_scope
(Function)
(Activation)
return function ()
{x += 1; return x;};})
3.1 x += 1
(Activation)
(0)
> f()
21
1
local variables
22
avoiding pollution
s = 0;
0;
for (i = 0; i < a.length; i += 1) s += a[i];
a[i];
return s;}
s;}
function...
6
how to fix it?
> s
s
ReferenceError
6 why does this work?
> i
i
ReferenceError
23
argument mismatch
when arguments are
› missing: initialized to undefined
› extra: ignored
> inc(1)
> inc(1,2)
> inc(1,2,3)
24
var decls
s = 0;
return s;}
function...
var s = 0;
return s;}
function...
var f = function(){
if (true) {
} else { › ECMA: 2
function g() { return 2;}; › Safari, Chrome: 3
}
var g = function() { return 3;}
› Mozilla: 4
return g();
26
lexical vs dynamic scoping
27
a language design question
x = 1;
g = function(){ console.log(x); x=2; }
f = function(){ var x = 3; g(); }
f();
console.log(x);
› harder to implement
28
a common misunderstanding
29
lookup at activation time
return result;
???
30
summary
functions are first-class
› values created by expressions
› bound to variables
› stored as properties, and can have properties
lexical closures
› free variables bound in ‘declaration’ environment
local vars
› added to local environment, just like function args
next
› exploiting functions & closures in programming
31
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.