Slide 1

Slide 1 text

Forgotten funky functions @jakobmattsson

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

ALL CHARACTERS AND EVENTS IN THIS SHOW-- EVEN THOSE BASED ON REAL PEOPLE--ARE ENTIRELY FICTIONAL. ALL CELEBERTY VOICES ARE IMPERSONATED.....POORLY. THE FOLLOWING PROGRAM CONTAINS COARSE LANGUAGE AND DUE TO ITS CONTENT IT SHOULD NOT BE VIEWED BE ANYONE

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Smorgasbord

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

1 Function => Function

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 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 14

Slide 14 text

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

Slide 15

Slide 15 text

(function, function) => function

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

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(); // ”init” is 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

var names = argNames(fullName); // [’firstName’, ’lastName']

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

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

Slide 33

Slide 33 text

Don’t try this at 127.0.0.1

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

2 Meta-programming eval Function.toString()

Slide 38

Slide 38 text

3 There is no class

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

class constructor extend(s) super CoffeeScript and ECMAScript 6

Slide 41

Slide 41 text

Everyone forgot Object.create

Slide 42

Slide 42 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 43

Slide 43 text

But overloading?!

Slide 44

Slide 44 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 45

Slide 45 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 46

Slide 46 text

So what is Object.create really?

Slide 47

Slide 47 text

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

Slide 48

Slide 48 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 49

Slide 49 text

3 There is no class Object.create

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Forgotten funky functions @jakobmattsson