fp and aop in javascript - infrequently · fp and aop in javascript or: “the little functional...

31
FP and AOP in JavaScript Or: “The Little Functional Language That Could” 1

Upload: vananh

Post on 29-Jun-2018

249 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: FP and AOP in JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

FP and AOP in JavaScriptOr: “The Little Functional Language That Could”

1

Page 2: FP and AOP in JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

You Probably Hate JavaScript

2

Most hackers do

Page 3: FP and AOP in JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

Browsers Suck

3

But buried down in them is a pretty cool little language

Page 4: FP and AOP in JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

“We use it because we have to”

4

Page 5: FP and AOP in JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

Never What You Expect

5

Page 6: FP and AOP in JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

Animal? Vegetable? Mineral?

6

Page 7: FP and AOP in JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

Most succesful scripting language ever?

8

Page 9: FP and AOP in JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

Modern Programming In JS

12

The whirlwind tour

Page 13: FP and AOP in JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

JavaScript Does OO!

13

...but it sure looks weird

Page 14: FP and AOP in JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

Functional Programming in JS

17

Page 18: FP and AOP in JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

Aspect Oriented JavaScript

24

Page 25: FP and AOP in JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

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 JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

Questions?

30

These slides available at:

http://alex.dojotoolkit.org/

Page 31: FP and AOP in JavaScript - Infrequently · FP and AOP in JavaScript Or: “The Little Functional Language That Could ... Functional Programming in JS 17. Functional Building Blocks

Thanks For Supporting Open Source!

31