Slide 1

Slide 1 text

Forgotten funky functions http://jakobm.com @jakobmattsson

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Startup hedge fund?

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Far from perfect and not for everything* or everyone * no, really

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Smorgasbord

Slide 9

Slide 9 text

1 Function => Function 2 Meta-programming 3 There is no class

Slide 10

Slide 10 text

1 Function => Function

Slide 11

Slide 11 text

Error handling You just divided by zero, didn’t you?

Slide 12

Slide 12 text

get('/users/123', function(err, response) { if (err) { cb(err); return; } cb(null, response.firstName); });

Slide 13

Slide 13 text

get('/users/123', p(cb, function(response) { cb(null, response.firstName); }));

Slide 14

Slide 14 text

var propagate = function(onErr, onSuccess) { return function(err) { if (err) { return onErr(err); } else { var slice = Array.prototype.slice; var rest = slice.call(arguments, 1); return onSuccess.apply(this, rest); } }; };

Slide 15

Slide 15 text

propagate = (onErr, onSucc) -> (err, rest...) -> if err then onErr(err) else onSucc(rest...) CoffeeScript ConferenceScript

Slide 16

Slide 16 text

(function, function) => function

Slide 17

Slide 17 text

(function, function) => function Need to solve a JavaScript problem? Add on one more function!

Slide 18

Slide 18 text

var once = function(f) { var called = false; var result = null; return function() { if (!called) { result = f.apply(this, arguments); called = true; } return result; }; };

Slide 19

Slide 19 text

var initOnce = once(init); initOnce(); initOnce(); initOnce(); // only called once...

Slide 20

Slide 20 text

var stateful = function(f) { var state = {}; return function() { var slice = Array.prototype.slice; var args = slice.call(arguments, 0); return f.apply(this, [state].concat(args)); }; };

Slide 21

Slide 21 text

var once = stateful(function(state, f) { return function() { if (!state.called) { state.result = f.apply(this, arguments); state.called = true; } return state.result; }; });

Slide 22

Slide 22 text

1 Function => Function call apply arguments slice bind …and closures!

Slide 23

Slide 23 text

2 Meta-programming

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

var fullName = function(firstName, lastName) { return firstName + " " + lastName; }; fullName('Jakob', ’Mattsson’); // ”Jakob Mattsson”

Slide 26

Slide 26 text

var fullName = function(firstName, lastName) { return firstName + " " + lastName; }; fullName('Jakob', ’Mattsson’); var fullName2 = argsAsObject(fullName); fullName2({ firstName: ’Jakob', lastName: ’Mattsson' });

Slide 27

Slide 27 text

// foo.com/fullName2?firstName=J&lastName=M funcs[req.path](req.query);

Slide 28

Slide 28 text

var argNames = function(f) { var reg = /\(([\s\S]*?)\)/; var head = reg.exec(f)[1]; var argCands = head.split(/[ ,\n\r\t]+/); var names = argCands.filter(function(x) { return x; }; return names; };

Slide 29

Slide 29 text

var argsAsObject = function(f) { var names = argNames(f); return function(args) { var argValues = names.map(function(name) { return args[name]; }); return f.apply(this, argValues); }; };

Slide 30

Slide 30 text

var fullName2 = argsAsObject(fullName); fullName2({ firstName: ’Jakob', lastName: ’Mattsson' });

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

fullName = renameArgs(fullName, [ ’first_name’, ’last_name’ ]); var fullName2 = argsAsObject(fullName); fullName2({ first_name: ’Jakob', last_name: ’Mattsson' });

Slide 33

Slide 33 text

var renameArgs = function(__uniq, argNames) { var parts = [ "(function(", argNames.join(', ') ") { " "return __uniq.apply(this, arguments);" "});" ]; return eval(parts.join('')); };

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

fullName2({ firstName: 'Jakob', lastName: 'Mattsson' }); var fullName = spreadArguments(fullName2, [ 'firstName', 'lastName' ]); fullName('Jakob', 'Mattsson'); // => 'Jakob Mattsson' Challenge!

Slide 36

Slide 36 text

2 Meta-programming eval Function.toString()

Slide 37

Slide 37 text

3 There is no class

Slide 38

Slide 38 text

new MyConstructor MyConstructor.prototype … and what about types?? Original JavaScript

Slide 39

Slide 39 text

class constructor extend(s) super CoffeeScript and ECMAScript 6

Slide 40

Slide 40 text

Everyone forgot Object.create

Slide 41

Slide 41 text

var Animal = { greet: function() { return this.speak() + ” says ” + this.name; } }; var Dog = Object.create(Animal); Dog.speak = function() { return ”woof”; }; var zelda = Object.create(Dog); zelda.name = ”zelda”; console.log(zelda.greet());

Slide 42

Slide 42 text

But overloading?!

Slide 43

Slide 43 text

var Animal = { greet: function() { return this.speak() + " says " + this.name; } }; var Dog = createAndInit(Animal, { speak: function(superSpeak) { return "woof"; }, greet: function(superGreet) { return superGreet() + ", who is a dog"; } });

Slide 44

Slide 44 text

var createAndInit = function(source, props){ var slice = Array.prototype.slice; var newObj = Object.create(source); Object.keys(props).forEach(function(propName){ newObj[propName] = function(){ var zup = source[propName]; var boundZuper = zup ? zup(this) : null; var newFunc = props[propName]; var args = slice.call(arguments, 0); var allArgs = [boundZuper].concat(args); return newFunc.apply(this, allArgs); }; }); return newObj; };

Slide 45

Slide 45 text

So what is Object.create really?

Slide 46

Slide 46 text

Object.create = function(x) { function F() {} F.prototype = x; return new F(); }; new MyConstructor MyConstructor.prototype

Slide 47

Slide 47 text

"I have been writing JavaScript for 14 years now (...). The super idea is fairly important in the classical pattern, but it appears to be unnecessary in the prototypal and functional patterns. I now see my early attempts to support the classical model in JavaScript as a mistake.” http://www.crockford.com/javascript/inheritance.html

Slide 48

Slide 48 text

3 There is no class Object.create

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

Forgotten funky functions http://jakobm.com @jakobmattsson