Академический Документы
Профессиональный Документы
Культура Документы
studio
functions, scope & closures
Daniel Jackson
functions as values
making functions
function expression
function (args) {body}
functions are polymorphic
implicit typed
depends on how args used
> three
> three()
> id(3)
> id(true)
true
> id(id)
> (id(id))(3)
> seq.c = 0
0
note: bad lack of
> seq()
encapsulation! will fix
1
later with closures
> seq()
2
> seq.c = 0
> seq()
> seq()
recursion
can you explain
how a recursive definition works?
fact(i-1);}
24
evaluating functions
two phases
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
> f()
> x = 2
> f()
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
evaluated in context
of functions scope
an object model
_proto
Activation
stmt
!
Statement
context !
Environment
creates
_scope
?
_body ?
?
Function
scope of a function
is context it was
created in
aah, nostalgia!
Courtesy of Harold Abelson and Gerald Jay Sussman. Used with permission.
From Structure and Interpretation of Computer Programs.
12
examples
13
example 1
x;}
function () {return x;}
x;}
1
> x = 1
1
1
> f()
f()
1
> x = 2
f()
> f()
simulating example 1
var
f = function () {return x;}
(Statement)
stmt
2. x = 1
(Activation)
context
creates
(Env)
bindings
_scope
x
(Var)
(Binding)
value
21
(Object)
(Function)
3. f()
(Activation)
body
return x;
(Statement)
stmt
stmt
3.1 return x;
(Activation)
4. x = 2
(Activation)
5. f()
(Activation)
5.1 return x;
(Activation)
example 2
x;}
function (x) {return x;}
x;}
1
> x = 1
1
1
> y = 2
2
2
2
f(y)
> f(y)
simulating example 2
stmt
bindings
(Env)
_scope
2. x = 1
(Activation)
var
x
(Var)
value
1
(Object)
(Binding)
bindings
(Function)
var
(Binding)
3. y = 2
(Activation)
return x;
(Statement)
4. f(y)
(Activation)
stmt
value
_proto
body
4.1 return x;
(Activation)
y
(Var)
2
(Object)
(Env)
var
x
(Var)
value
2
(Object)
(Binding)
> 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()
simulating example 3
1. x = 1
(Activation)
f = (function (x) {
return function ()
{return x;}}) (x)
(Statement)
stmt
context
(Env)
bindings
var
x
(Var)
value
21
(Object)
var
x
(Var)
value
1
(Object)
(Binding)
2. f = ...
(Activation)
_scope
2.1 return function ...
(Activation)
(Function)
creates
_proto
(Env)
_scope
(Function)
3. x = 2
(Activation)
body
4. f()
(Activation)
4.1 return x
(Activation)
bindings
(Binding)
> x = 1
1
> f = (function (x) {
return function ()
{return x;};}) (x)
function () {return x;}
> x = 2
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 ()
{x += 1; return x;};}) (0)
(Statement)
stmt
context
1. f = ...
(Activation)
creates
(Env)
_scope
(Function)
_proto
creates
(Env)
_scope
bindings
var
x
(Var)
value
01
(Object)
(Binding)
(Function)
3. f()
(Activation)
3.1 x += 1
(Activation)
body
return function ()
{x += 1; return x;};})
(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...
function...
whats
wrong
with
this
function?
> sum([1,2,3])
sum([1,2,3])
6
> s
s
ReferenceError
6
why does this work?
> i
i
ReferenceError
23
argument mismatch
when arguments are
missing: initialized to undefined
extra: ignored
> inc = function (x, y) {return y ? x+y : x+1;}
> inc(1)
> inc(1,2)
> inc(1,2,3)
24
var decls
s = 0;
return s;}
function...
var s = 0;
return s;}
function...
25
function declarations
function declaration syntax
var f = function(){
if (true) {
} else {
function g() { return 2;};
}
var g = function() { return 3;}
return g();
ECMA: 2
Safari, Chrome: 3
Mozilla: 4
26
27
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
return result;
???
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.