![Page 1: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/1.jpg)
FP and AOP in JavaScriptOr: “The Little Functional Language That Could”
1
![Page 2: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/2.jpg)
You Probably Hate JavaScript
2
Most hackers do
![Page 3: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/3.jpg)
Browsers Suck
3
But buried down in them is a pretty cool little language
![Page 4: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/4.jpg)
“We use it because we have to”
4
![Page 5: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/5.jpg)
Never What You Expect
5
![Page 6: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/6.jpg)
Animal? Vegetable? Mineral?
6
![Page 7: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/7.jpg)
Whatever You Want It To BeImperative to imperative programmers
OO (with quirks) to OO programmers
Functional to FP hackers
7
![Page 8: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/8.jpg)
Most succesful scripting language ever?
8
![Page 9: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/9.jpg)
UbiquityServer-side
Rhino and JDK 1.6 (Java)
Spidermonkey and KJS (C/C++)
ASP classic (JScript.NET is not JavaScript)
Client-side
Every modern browser
OSes and desktops
WSH, KJS, Konfabulator, Dashboard
Others: Adobe Acrobat, Flash 8.5 (ActionScript 3), etc.
9
![Page 10: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/10.jpg)
Language Background
10
First version in 1995 for Netscape 2.0
Developed by Brendan Eich as “LiveScript”
Renamed “JavaScript” as a marketing exercise
Standardized via ECMA in 1997, ISO in 1998
Not well understood
Heaviest users have lacked programming backgrounds
Easy to pigeon-hole as one “kind” of language
Ajax/DHTML/JS community now on 3rd generation libraries
![Page 11: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/11.jpg)
Language FeaturesLexical scope
Functions are objects
Functions are closures
Anonymous function support
Delegation via the prototype chain
OOP via constructor closures and prototype inheritance
Some interpreters support continuations
C-style syntax
11
![Page 12: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/12.jpg)
Modern Programming In JS
12
The whirlwind tour
![Page 13: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/13.jpg)
JavaScript Does OO!
13
...but it sure looks weird
![Page 14: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/14.jpg)
Prototype-Based Inheritance
14
function FooClass(ctorArg1, ctorArg2){}
FooClass.prototype.foo = “foo”;FooClass.prototype.bar = “bar”;
var baz = new FooClass();baz.foo = “my own foo”;
var thud = new FooClass();thud.foo == “foo”;
![Page 15: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/15.jpg)
Singletons
15
namespace.singleton = new function(){ var privateVar = 0; this.increment = function(){ return privateVar++; }
function privateMethod(){ } this.publicWrapper = function(){ return privateMethod(); }}
![Page 16: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/16.jpg)
No Interfaces...Mixins!
16
// “interfaces that actually do something”namespace.mixinClass = function(){ this.foo = “foo”; this.bar = function(){ alert(this.foo); }};function thingerClass(){ namespace.mixinClass.call(this);}(new thingerClass()).foo == “foo”;
![Page 17: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/17.jpg)
Functional Programming in JS
17
![Page 18: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/18.jpg)
Functional Building BlocksJavaScript may or not be “functional”
Depends on who you ask and what time of day it is
JavaScript 1.6 adds stronger primitives
Array.map(), Array.filter(), Array.some(), Array.every()
Until 1.6 arrives, we can roll our own
“Arrives” usually means “95+% of browsers support it”
Upgrade trajectories suggest half a decade
Downside to heterogeneous ubiquity
18
![Page 19: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/19.jpg)
JavaScript Closures
19
function foo(){ var bar = 1; return function(){ return bar++; };}var baz = foo();baz() == 1; baz() == 2;var thud = foo();thud() == 1; thud() == 2;
![Page 20: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/20.jpg)
JavaScript Monads(with aplogies to Shannon Behrens)
20
function MonadClass(value){ this.value = value || undefined;}MonadClass.prototype.pass = function(value, cb, scope){ if(typeof value[”value”] == “undefined”){ return new this.constructor(); } // side effects go here! if(scope){ return cb.call(scope, value); } return cb(value);}
![Page 21: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/21.jpg)
Curried JavaScript
21
// load(”dojo.js”);dojo.require(”dojo.lang.*”);
function foo(bar, baz, thud){ alert(”got at least 3: ”+arguments.length);}var tmp = dojo.lang.curry(foo, “one”);tmp(”two”, ”three”); // alertstmp(”two”)(”three”); // also alertstmp(”two”, “three”, “four”); // passes 4 args to foo
![Page 22: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/22.jpg)
Curried JavaScript (contd.)
22
// this time with objects and methodsvar foo = { bar: function(one, two){ alert(”arguments: “+arguments.length); }};var tmp = dojo.lang.curry(foo, “bar”, “one”);tmp(”two”, “three”, “four”); // alerts 4tmp(”huzzah!”); // calls foo.bar(), alerts 2
![Page 23: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/23.jpg)
Dojo: A Standard Library for JSFunctional wrappers
dojo.lang.map
dojo.lang.filter
dojo.lang.every (like Python itertools.all)
dojo.lang.some (like Python itertools.any)
dojo.lang.unnest
dojo.lang.curry
no “zip” or “reduce” (no user request for them)
23
![Page 24: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/24.jpg)
Aspect Oriented JavaScript
24
![Page 25: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/25.jpg)
AOP in JavaScriptPre-processor is unworkable
“Refresh” is “make all” for the web
Runtime AOP is possible
Function objects can be “moved”
Wrapper functions (joinpoints) inserted in their place
Closures allow joinpoints to maintain state
dojo.event.connect() implements joinpoints and advice
Pointcuts and queries not currently implemented
25
![Page 26: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/26.jpg)
dojo.event.connect()Provides following advice types
before
before-around
around
after
after-around
Normalized interface for browser DOM and JavaScript calls
Fixes memory leaks on IE
26
![Page 27: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/27.jpg)
Syntactic WorkaroundsCan’t retrieve parent Object from Function Object
Reference to “foo.bar” will not allow you to determine foo
JavaScript hash/dot duality to the rescue!
foo.bar == foo[”bar”]
Therefore:
connect(foo, “bar”) == connect(foo, foo.bar)
27
![Page 28: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/28.jpg)
Events for Everyone!
28
var tmpNode = dojo.byId(”someNode”);
var foo = { bar: function(){} };
dojo.event.connect(tmpNode,“onclick”, foo, “bar”);
dojo.event.connect(foo, “bar”, function(){
alert(”foo.bar was called”);
});
// clicking on the node now will throw an alert
![Page 29: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/29.jpg)
AOP Events (contd.)
29
var foo = { bar: function(){ alert(”foo.bar”); } };
var baz = { qux: function(){ alert(”baz.qux”);} };
dojo.event.connect(”before”,
foo, “bar”,
baz, “qux”);
foo.bar(); // alerts “baz.qux” and then “foo.bar”
![Page 30: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/30.jpg)
Questions?
30
These slides available at:
http://alex.dojotoolkit.org/
![Page 31: FP and AOP in JavaScriptinfrequently.org/wp-content/AOP_and_FP_in_JS.pdf · Functional Building Blocks JavaScript may or not be “functional” Depends on who you ask and what time](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec98229565d3e44ea2a4feb/html5/thumbnails/31.jpg)
Thanks For Supporting Open Source!
31