FUNCTION OF FUNCTIONS


Functions, functions in any programming language whether it is for web Dev or android Dev or for anything out there, are the small named codes that are encapsulated in the source codes which are supposed to perform some specific tasks, and they also provides modularity to the code of course.
functions are mainly of two types generally(considering most of the programming languages).
  • functions which return a value, includes return.
  • functions which would not return a value.
Moving on, considering particularly NODE.JS contains some special functions few of them i am covering now:
  • simple function which returns something.
  • immediately executing function/self executing functions.
  • anonymous functions.
  • higher-order function(setTimeout()).
SIMPLE FUNCTIONS WHICH RETURN SOMETHING.
functions with simple function definition, and with a return statement. BTW return statement is not necessary but function still returns something which in this case would be “undefined”.
example: function function_name()
{ return 1947; } // returns a value every time this function is called.
console.log(function_name()); // prints 1947
// UNDEFINED RETURN EXAMPLE
function function_name2()
{ } // still returns undefined.
console.log(function_name2()); // returns undefined
IMMEDIATELY EXECUTING FUNCTION/SELF EXECUTING FUNCTIONS.
these functions are defined inside a parentheses, followed by pair of parentheses and a semicolon. These functions are immediately executed after there definition.
example: (function self_executing()
{ console.log(“in the self executing function”); })(); //notice parentheses
another important use of these functions is to limit scope of a certain variable inside themselves, lets take an example;
// want to use a different value of same variable again in if else clause
// simple program.
var again=”medium.com”; // assigning again a value
if(true) // always executes
{ again=”medium.in”; // re-assigning value
console.log(again); } // prints “medium.in”
console.log(again); // prints “medium.in”
// using self executing function.
var again=”medium.com”; // assigning
if(true)
{ (function goodgame() //self executing function
{ again=”medium.in”; // new scope created in function
console.log(again); //prints “medium.in”
})();
}
console.log(again); // prints “medium.com”
ANONYMOUS FUNCTIONS.
functions which aren’t given a name but is assigned to a variable, and it can be used to call that function.
example: var function_name = function() // no name given accessed by variable name.
{ console.log(“anonymous function”); }
function_name(); // prints “anonymous function”
HIGHER-ORDER FUNCTION.
functions which takes other functions as their arguments. setTimeout() functions simply does some assigned task after some time that is fixed in mili-seconds units.
example:
setTimeout(function(){ console.log(“appears 2 sec. later”); },2000);
// or same can be done as,
function time_it()
{ console.log(“appears 3 sec. later”); }
setTimeout(time_it, 3000);
despite of being a function(test_it()) there should be no parenthesis in the parameter area of higher order functions.
//FIN;

Comments

Popular posts from this blog

CSRF #1 {request forgery}

CTF WRITEUPS : Birdman's Data {Network}

BANDIT OVERTHEWIRE.org writeup.