Home page HTML i javascript C++ Hardware HTML i javascript Java Software SQL

Javascript - variables in which are the functions.

Added: 2012-02-24

Javascript offering some interesting opportunities, what is placing the function in the variable. This variable stores the code of function, that may be running.

Value that variable you can easy way display using window alert, if you want to run it just add the brackets [varibleName]().

If you want to check if a variable is a function, you can do this using typeof [variableName] if yes, then you see result function.

If you want to view the code of the function saved in the variable, and perform any operations on it, first you must convert it to string: var temp = new String([variableFunction]);

Example whose gets name function with the variable, bellow:
function getNameFunctionInVariable(varFun){

	if (typeof varFun == "function"){
		var nameFun = new String(varFun);
		nameFun = nameFun.substr(8);
		nameFun = nameFun.substr(0, nameFun.indexOf('('));
		nameFun = nameFun.replace(/ /g, "");
		nameFun = nameFun.replace(/	/g, "");
	} else
		return varFun;
	return nameFun;

If you saved name function in variable, and you want to run it use: window[[variableName]]();