Search code examples
javascriptnode.jschainingfloating-action-button

Javascript FAB framework on Node.js


I've seen a slide that presented Fab, a node.js framework.

Fab slide

Is this JavaScript?

Could someone explain what is going on in that code?

I'm all lost.


Solution

  • Is plain JavaScript, it is a function chaining pattern.

    The first line, ( fab = require("fab") ) includes the fab function and returns a reference to it.

    All the subsequent parentheses are function calls, each function invocation returns probably the same function again and again.

    The pattern probably looks like this simplified example:

    var foo = function (arg) {
      // detect what the argument is
      if (typeof arg == 'function') {
        // do something with arg
        console.log('function: '+arg());
      } else if (arg instanceof RegExp) {
        // arg is a RegExp...
        console.log('A RegExp: '+arg);
      } else if (typeof arg == "string") {
        // arg is a string
        console.log('A string: '+arg);
      }
      return foo; // return a reference to itself
    };
    
    (foo)
      (function() { return "Foo "; })
      (/bar/)
      (" baz!");
    

    Outputs:

    function: Foo
    A RegExp: /bar/
    A string: baz!