JavaScript - Some Tips

The difference of JSON and object:

  • JSON can't have function.
  • JSON can't have circle reference,key and value must be in double quotes.

Lambda

A lambda is a function that is used as data.
Lambdas are treated like data that can be passed around as inputs and outputs between other functions, regardless of whether or not they are named.
If a function is used as an argument or return value, it's a lambda.

IIFE

Immediately Invoked Function Expression IIFE
This technique is often used to create a new scope to encapsulate modules.

//a1 === arg1, a2 === arg2
(function(a1, a2){
}(arg1, arg2));

Hoist

Guess the output of console.log in below code.

var x = 1;

(function () {
  console.log(x);
  var x = 2;
}());

Yes, it is undefined. After hoisted, the code would look like

var x = 1;

(function () {
  var x; // Declaration is hoisted and x is undefined.
  console.log(x);
  x = 2; // Initialisation is still down here. 
  // This also is why the function expression is not hoisted.
}());

Functional

Partial application wraps a function that takes multiple arguments and returns a function that takes fewer arguments.

Currying is the process of transforming a function that takes multiple arguments into a chain of functions, each of which takes no more than one argument. An add function add(1, 2, 3) would become add(1)(2)(3) in curried form, where the first call returns a function that returns another function, and so on.