why the result of this function not assigning value of 1 [duplicate] - javascript

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).

Related

What is the meaning of returning a function from another function? [duplicate]

This question already has answers here:
Functions that return a function
(10 answers)
How do JavaScript closures work?
(86 answers)
Closed 1 year ago.
What is the meaning of returning a function from another function in javascript. Is this is same as if I call a function which is in another function
function fun(c){
let a=3;
return function(){
return a+c;
}
}
let add=fun(2);
console.log(add);
. And my second question is why this add variable is a function.
Currently, the object which you are returning is the function.
In this context, variable add contains a reference to the function which you have to invoke.
This is called currying. It allows you to apply arguments to functions one by one.
Example:
const add = a => b => a + b // Function that takes `a` and returns a function that adds `a` to `b`
const addThree = add(3) // bind `a` to 3. This is a function that takes `b` and adds 3 to it
console.log(addThree(1)) // now `b` is 1, it's gonna be added to `a` and return the final value
There's a lot of literature about currying and partial application. Google is your friend

How to change the value of a passed variable in a function in javascript? [duplicate]

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

Don't understand a construct in a JS Decorator [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 3 years ago.
I was studying on JS Decorators and don't understand how the wrapper is able to access the inner functions argument. The snippet works, but I don't understand why the anonymous function 'function(val)' gets access to val, the argument of slow().
// https://codepen.io/lideebe/pen/VOjGvb
// simple function that gets wrapped
function slow(x){
return x * 3;
}
// the wrapper function
function cacheDecorator(func){
return function(val){ // How does this anonymous function get access to val?
return func(val)
}
}
// do the wrap
slow = cacheDecorator(slow);
// call the function
console.log(slow(2));
Output is 6 and that is correct.
It's because cacheDecorator returns a function which takes a parameter val, and when you call that returned function with the value 2, it just gets accessed like normal. So slow, after you reassign it, is:
slow = function(val) {
return val * 3;
}
So val is used as the parameter for the argument passed when the newly wrapped function is called.

String passed as function parameter is undefined [duplicate]

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.

how to pass by reference in javascript [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 8 years ago.
How should I pass by reference in a JavaScript function?
For example:
function passByReference(a){
a = "banana";
}
var x = "apple";
passByReference(x);
Here x should output banana.
I am new to JavaScript; any help would be appreciated. Thanks in advance.
Wrap the variable with an object. Properties of objects are passed by reference.
function passByReference(a) {
a.fruit = 'banana';
}
var wrapper = {fruit: 'apple'};
passByReference(wrapper);
You cannot pass by reference. If you want to modify the state of your caller from within the called function, there are two ways you could do:
Sometimes it fits best to wrap the state in an object:
var x = { valueToChange: 'initialValue' };
passByReference(x);
This works because with objects, a pointer to the address where the object lies is passed. That pointer is passed by value but it still points to the same object.
In some other cases a callback does the trick:
var x = "apple";
passByReference(function (newValue) { x = newValue; });
function passByReference(callback) {
callback("banana");
}
This works because if you define a function as above it builds a closure together with all variables it references. When your function gets called it really modifies the value of x.

Categories