Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript FAB framework on Node.js

Tags:

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.

like image 952
never_had_a_name Avatar asked Sep 26 '10 18:09

never_had_a_name


People also ask

Can I use JavaScript in node JS?

Node. js allows you to run JavaScript on the server.

What framework does node js use?

NestJS: It is a Node. js framework used for building server-side applications that are efficient in every way. Apart from using JavaScript, it also supports TypeScript along with certain elements of Functional Programming (FP), Object-Oriented Programming (OOP) and Function Reactive Programming (FRP).

Is node js a web framework which use JavaScript library?

The Node. js runtime is built on top of a programming language—in this case, JavaScript—and helps in running frameworks itself. To sum up, Node. js is neither a programming language nor a framework; it's an environment for them.

What is NEST JS framework?

NestJS is a progressive Node. js framework that helps build server-side applications. Nest extends Node. js frameworks like Express or Fastify adding modular organization and a wide range of other libraries to take care of repetitive tasks. It's open-source, uses TypeScript, and is a very versatile Node.


2 Answers

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!
like image 62
Christian C. Salvadó Avatar answered Sep 18 '22 17:09

Christian C. Salvadó


That's hard to follow indeed; it doesn't really look like Javascript at all...

Anyway, FAB takes advantage of returning a pointer to the function which was called. For example:

function doSomething(str){
  alert(str);
  return arguments.callee;
}

// Alerts 'hi' and then 'there'
doSomething('hi')('there');

Of course you can implement extra conditions, like counting the number of arguments or checking the type of arguments passed in. For example:

function doSomething(){
  if(arguments.length == 1){
    alert(arguments[0])
  } 
  else if(arguments.length == 2){
    alert(arguments[0] + arguments[1]);
  }

  return arguments.callee;
}

doSomething
  ("Hi, 3 + 4 is:")
  (3, 4);

The last example alerts:

> Hi, 3 + 4 is:
> 7
like image 35
Harmen Avatar answered Sep 17 '22 17:09

Harmen