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());
Related
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 2 years ago.
Can anyone tell me what I can replace the evil eval with in this code?
var x = function(funcName) {
function funcA () {
console.log("a");
}
function funcB () {
console.log("b");
}
var funcToRun = eval(funcName);
return funcToRun();
};
x("funcA");
x("funcB");
I've see a bunch of different solutions, but none seem to fit this scenario. Essentially, I need to be able to pass a string into a function as a "config" of which sub-function to run. I don't have access to the code that calls x, I've just been instructed that only primitive and string values can be configured in the call.
P.S. This is a super simplified version of the actual function.
Thanks!
James
You could just create an object and add those functions as properties that you can then access with a string.
var x = function(funcName) {
function funcA() {
console.log("a");
}
function funcB() {
console.log("b");
}
const fs = {
funcA,
funcB
}
var funcToRun = fs[funcName]
return funcToRun();
};
x("funcA");
x("funcB");
This question already has answers here:
How does "this" keyword work within a function?
(7 answers)
Closed 5 years ago.
I have a problem receiving a value from a void method
For example:
test() {
var x = this.test2("hi there");
console.log(x);
}
test2(data){
return data;
}
I want to receive the data from test2 but it keep saying undefined what do I do wrong in here? And how can I make this work?
It is probably so basic but I just want to know why I receive the value undefined
Add function in front of definition.
function test() {
var x = this.test2("hi there");
console.log(x);
}
function test2(data) {
return data;
}
test();
Try like this :
test(): void {
var x = this.test2("hi there")
console.log(x);
}
test2(data): void {
return data
}
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);
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();
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() {
}
}());