Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Forgotten Funky Functions

Forgotten Funky Functions

Given at JsDay in Verona 2016-05-11

Jakob Mattsson

May 11, 2016
Tweet

More Decks by Jakob Mattsson

Other Decks in Programming

Transcript

  1. 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
  2. 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); } }; };
  3. propagate = (onErr, onSucc) -> (err, rest...) -> if err

    then onErr(err) else onSucc(rest...) CoffeeScript ConferenceScript
  4. var once = function(f) { var called = false; var

    result = null; return function() { if (!called) { result = f.apply(this, arguments); called = true; } return result; }; };
  5. 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)); }; };
  6. var once = stateful(function(state, f) { return function() { if

    (!state.called) { state.result = f.apply(this, arguments); state.called = true; } return state.result; }; });
  7. var fullName = function(firstName, lastName) { return firstName + "

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

    " + lastName; }; fullName('Jakob', ’Mattsson’); var fullName2 = argsAsObject(fullName); fullName2({ firstName: ’Jakob', lastName: ’Mattsson' });
  9. 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; };
  10. 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); }; };
  11. var fullNameSnake = renameArgs(fullName, [ ’first_name’, ’last_name’ ]); var fullName2

    = argsAsObject(fullNameSnake); fullName2({ first_name: ’Jakob', last_name: ’Mattsson' });
  12. var renameArgs = function(__uniq, argNames) { var parts = [

    "(function(", argNames.join(', ') ") { " "return __uniq.apply(this, arguments);" "});" ]; return eval(parts.join('')); };
  13. fullName2({ firstName: 'Jakob', lastName: 'Mattsson' }); var fullName = spreadArguments(fullName2,

    [ 'firstName', 'lastName' ]); fullName('Jakob', 'Mattsson'); // => 'Jakob Mattsson' Challenge!
  14. 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());
  15. 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"; } });
  16. 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; };
  17. Object.create = function(x) { function F() {} F.prototype = x;

    return new F(); }; new MyConstructor MyConstructor.prototype
  18. "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