27 February 2017

Javascript | Basic Knowledge

IIFE

IIFE or 'immediately invoked function expression' is basically an anonymous function wrapped in parenthesis.

add function is private and publicTest is a public function
variable x is still exist even though the anonymous function has already returned. This is possible thanks to the power of 'closure'. All variables inside the IIFE are private and the functions inside the return statement are public.
IIFE can also have arguments like this:
Go to browser console and call 'hello' to see the output

Closures

In Javascript you can have nested functions or function inside a function. The inner functions will have access to all variables of the parent function. This feature, combined with IIFE build another Javascript feature called 'closure'.

Modules

Javascript doesn't natively support classes. To implement classes in Javascript, we use modules. To create a module, we combine self-invoking function (IIFE), encapsulation and closures altogether
Three modules in an application. The third module connected the other modules

Callback function

Callback function is basically a function that is passed to another function as a parameter or an argument. Do not attach the ( ) (function brackets) to the callback function, because the function will be called only if the parent function is called. So the calling process is done by the parent function. 
ctrlAddItem is passed as parameter. No function brackets needed.

No comments:

Post a Comment