How to call a JavaScript function using variable [duplicate] - javascript

This question already has answers here:
call a javascript function using string name
(2 answers)
Closed 5 years ago.
How to call JavaScript or jquery function using variable.
var fnName= "abc"; //how to use fnName as a function call where "abc" will be function name
function abc(){ //definition........ }

Define function globally.
First Way
Call as window[functionName]().
function abc() {
alert('test');
}
var funcName = 'abc';
window[funcName]();
Second Way Add function to defined object.
function parentFunc(name) {
var childFuncs = {
"abc": function() {
alert("test");
}
}
childFuncs[name]();
}
var funcName = 'abc';
parentFunc(funcName);

Related

Can we call the function by function name after assigning it to a variable.? [duplicate]

This question already has answers here:
Javascript functions like "var foo = function bar() ..."?
(9 answers)
Closed 6 years ago.
Have a function assigned to a variable
var sq = function a(s){return s * s};
Now if i call sq(4) i am getting the result as 16 but if i try to call using the function name a(4) it throws an error "a is not defined"
Can we not call the function by its name once it is assigned to a variable.?
No, because you're using an assignment. You can just do var fn = function () { } or var fn = ()=> { } and get the same thing.
If you created the function and then assigned it to a variable, you could do both.
Basically,
function fn() {...}
var myFunc = fn;
You could then do both:
myFunc();
fn();

Function call javascript [duplicate]

This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Closed 6 years ago.
I called helloworld and defined it with below two different ways:
1) With variable
2) With function name itseld
var helloWorld = function() {
return '2';
}
function helloWorld() {
return '1';
}
alert (helloWorld()); // This alert 2, but in absence of "var helloWorld = ....", it alert "1".
Can anyone please explain reason why it's calling var helloWord = ? and not function helloWorld () ?
Thank You !!
why it's calling var helloWord = ? and not function helloWorld () ?
Because functions definitions will be hoisted to the top. And the assignments remains in the same place. So it is getting overridden.
This is how the interpreter sees the code,
function helloWorld() {
return '1';
}
var helloWorld;
//the above function is getting overridden here.
helloWorld = function() {
return '2';
}
alert (helloWorld());

What's the difference between different method declarations in JavaScript? [duplicate]

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 8 years ago.
I have seen developers using different ways to declare/define functions in js,
like:
// 1
createview:function()
{
}
// 2.
var createview=function()
{
}
// 3.
function createview()
{
}
While 2nd and 3rd are function expression and declaration respectively, what's with the 1st usage?
Its an object and used for example in the revealing module pattern.
var module = (function() {
var stuff = function() { return 'stuff'; };
return {
stuff: stuff
}
}());
Now you have a name spaced closure.
module.stuff() //--> returns 'stuff'

How does the parenthesis at the end of a javascript function supposed to work [duplicate]

This question already has answers here:
What do parentheses surrounding an object/function/class declaration mean? [duplicate]
(7 answers)
Closed 8 years ago.
I consider myself a pretty strong javascript coder and am familiar with most all of the javascript syntax. But have been puzzled by the following syntax:
function() {
return function() {
}
} ();
Can someone explain what the parenthesis at the end is supposed to be used for?
So, the expression:
(function() {
return function() {
}
})
Evaluates to a function (without a name in this case) that returns some other function.
Adding ():
(function() {
return function() {
}
})();
Would simply call that function.
Another way to write this would be:
var foo = function() {
return function() {
}
};
foo();
It is a self invoking function. Meaning a function that declares and calls itself.
Another form would be:
(function() {
return function() {
}
}());

Syntax of variable declaration? var a = (function() { })() [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What do parentheses surrounding a JavaScript object/function/class declaration mean?
I have found the following code in a website .
var testModule = (function(){
var counter = 0;
return {
incrementCounter: function() {
return counter++;
},
resetCounter: function() {
console.log('counter value prior to reset:' + counter);
counter = 0;
}
};
})();
So it follows the syntax var a = (blah balh..)()
What does it actually mean? What is the meaning of variable declaration like a =()()..
It's defining a single-use function and executing it immediately. The code you provided is named the Module Pattern -- see here for more information about its properties: http://www.yuiblog.com/blog/2007/06/12/module-pattern/
A normal function might be created like this:
var f1 = function() {
console.log('bar');
};
And you could subsequently call it like so:
f1();
But in the example you provided, the function is both defined and executed once, and that function returns an object with two functions: incrementCounter and resetCounter. You can call them like so: testModule.incrementCounter() and testModule.resetCounter()
The Module Pattern is useful when you have a single object and you want to encapsulate some properties which are only available to the functions defined in the closure.
The anonymous function is executed and the return value is assigned to the variable.

Categories

Resources