This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 6 years ago.
When I pass a string to a function as a parameter, it returns undefined. Why is that?
let a = 'b';
let test = (a) => console.log(a);
test(); // undefined
When you call test(); you aren't putting anything between ( and ), so you aren't passing any parameters.
When you defined test (with (a) =>) you created a local variable a that masks the global variable with the same name.
To pass a you have to actually pass it: test(a).
Why is that?
Because you don't pass any argument. Try the following:
test(a);
The following definition:
let test = (a) => console.log(a);
is like the following:
function test(a){
console.log(a);
}
So when you call test, without passing any argument the value of a would be undefined.
Related
This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 2 years ago.
why the result of this function is not 1?
function call(t) {
t = 1;
};
var b = 0;
call(b);
console.log(b)
Parameters are passed by value, not by reference. From MDN:
Primitive parameters (such as a number) are passed to functions by value; the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function.
Because b is not directly accessed by the function when calling it. b is used as a parameter for the function, but it doesn't change the original varible.
The function is just changing the value, which is passed to the function inside the function. If you would like to change the variable b, you would need something like this:
var b = 0;
function call() {
b = 1;
}
call();
console.log(b);
In this case b is 1 after execution of the function (because it referenced the variable b directly).
This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 2 years ago.
class Hello{
constructor(member) {
this.member = member;
this.name_function_map = {"print_member" : this.print_member};
}
print_member(){
console.log(this.member);
}
}
let h = new Hello(2);
h.print_member();
//=> works as expected
h.name_function_map["print_member"]();
//=> why is it h. member undefined
the output is:
2
undefined
can someone please enlighten me, why is calling the member function of Hello through a reference stored in a map any different than calling it directly?
And how may i work around that.
When you execute:
let h = new Hello(2);
h.print_member();
The this keyword inside the print_member method will be equal to h (the object instance). That is correct. But, when you execute:
h.name_function_map["print_member"]();
or equally
h.name_function_map.print_member();
the this keyword inside the print_member method will be equal to h.name_function_map, which is:
{"print_member" : this.print_member}
Obviously, this object does not have a member property, and that's why you are getting undefined.
This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 3 years ago.
Inside a function, I'd like to change the value of a global variable that is specified by the function's argument.
A = 1;
B = 2;
C = 3;
function myFunction(variable) {
variable += 10;
}
myFunction(A);
myFunction(B);
myFunction(C);
I'm looking for these results:
console.log(A); // 11 expected
console.log(B); // 12 expected
console.log(C); // 13 expected
I can't output the new value via a return statement because it's computed inside the callback function of a post request.
How to achieve this?
It's not possible. This is called shadowing, that's that your argument is other variable than global, so A in myFunction is other that A in global
Maybe trying to run with apply() and using this.variable inside function would help
This question already has answers here:
Javascript functions like "var foo = function bar() ..."?
(9 answers)
Closed 3 years ago.
I'm new to JS, so please forgive me if this sounds stupid. I was playing with the concepts of function declaration and function expression.
I have the following code:
var printSomething = function printSomeString(string) {
console.log(string);
}
console.log(typeof printSomething); // function
console.log(typeof printSomeString); // undefined
If I go by the definition of hoisting in JavaScript, by the time I use printSomething and printSomeString, they should be available since their declarations have been hoisted.
typeof printSomething returns function, but typeof printSomeString returns undefined. Why so?
Isn't this named function expression already declared and hoisted before being used?
Isn't a named function expression itself a function?
Also, when I call printSomeString('Some STRING'), it returns the following
Uncaught ReferenceError: printSomeString is not defined
What is going on here?
printSomeString is a not a global variable its local variable to the another function printSomething. Try using console.log() inside it.
var printSomething = function printSomeString(string) {
console.log(typeof printSomeString)
}
console.log(typeof printSomething); // function
printSomething()
This question already has answers here:
Use JavaScript variable as function name?
(5 answers)
Closed 9 years ago.
I have a funcion whose name is stored in a variable , How can I invoke it?
var fun_name='foo'
If if function is available in global space
window[fun_name]()
If is a member of an object then obj[fun_name]()
Ex:
var obj = {
foo: function(){}
}
obj[fun_name]()
You can do that using.
window[fun_name]();
it will also have scope, so simply
scopeVar[fun_name](args);
or if global
window[fun_name](args);
You can use the "eval" function
This will call alert(3)
foo = 'alert(3)';
eval(foo);
Check this out.
How to execute a JavaScript function when I have its name as a string
Or simply use a switch like follows:
switch(fun_name){
case "foo":
foo();
break;
case "foo2":
foo2();
break;
default:
fooDefault();
}