Why the function's log is the function itself? [duplicate] - javascript

This question already has an answer here:
Why is the named IIFE logged instead of the variable with the same name?
(1 answer)
Closed 2 years ago.
I have encountered a problem that if the IFEE function's name is the same with variable's name in it. the output is the function itself. Why?
var b = 10;
(function b() {
b = 20;
console.log(b);
})();

Named function expressions create a read only variable in their own scope which matches their name and references themselves.
This is useful for writing recursive functions.

Related

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

Javascript -What happens if a variable and a function have the same name? [duplicate]

This question already has answers here:
Function and variable with the same name
(2 answers)
Closed 5 years ago.
Might be a kind of easy question, but I have a question on the issue of having the same name for a variable and a function.
If there's a variable,
var add = 1;
and a function,
function add(x,y) {return x+y;}
and there're two console.log,
console.log(add)
console.log(add(1,2))
I've expected those 2 console.log would work properly since add contains the Number and add() is classified as a Function, but the second one prints an error. So they aren't considered the same.
But the result says I'm wrong.
Can anyone explain what's going on in my code?
Variables and function definitions(not expressions) are hoisted to up, it means that wherever in scope you wrote your function or variable they will be moved to the start of the scope. First goes functions definitions then variables. So it means that functions will be overwritten by variables.
var add = 1;
function add(x,y) {return x+y;}
console.log(add);
The order doesn't matter. Later will be the variable and will overwrite
function add(x,y) {return x+y;}
var add = 1;
console.log(add);

Will JavaScript Closure save all variables in its scope, or only those the closure referred? [duplicate]

This question already has answers here:
Over how much of its enclosing scope does a (javascript) closure close?
(2 answers)
Closed 5 years ago.
We knew that
function foo () {
var x = 10;
var y = 20;
function bar () {
return x + 1;
}
bar(); // 11
}
The function bar create a closure, and save the reference of x.
But what about the variable y? Will the closure bar created hold its reference? I tried it in Chrome Dev Tools, and it shows only x in the [[Scopes]] field, without y. But I can not find any articles about that.
Does it means the closure creation will only pick what it need to save?
Scopes are chained together and it'll check with parent scope when reference cannot be find. This question is answered before and you can reference it here:
Scope Chain in Javascript

Does declaring a function create a variable with the function name and the function object assigned to it? [duplicate]

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 8 years ago.
Just want to clear up some confusion regarding functions in JavaScript. Does a declared function create a variable with the function name in the same function scope and assign the function object to itself?
By code,
function name(){}
does it translate to,
var name = function name(){}
just before execution? If function is an object it should be held some where inside the scope by reference right?
Essentially yes with one exception.
In both your examples, the name function will be assigned to the name variable. In your first example the name variable and function definition will be hoisted to the upper most part of the current scope.
In your second example the name variable will be hoisted to the upper most part of the current scope but the definition will remain at it's current position in code.

Javascript variable declaration, why is this legal? [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 8 years ago.
I just encountered this variable declaration syntax for the first time.
var myVar = (function() {
return 1;
})();
My 2 main questions are..
What is it called, and why is it legal?
Forgive me if this question has been asked before, I tried searching around but I have no idea what this notation is called so I wasn't able to find anything.
Also, I should add, what is the function of the 2 sets of parentheses? The first of which encloses the function, the second of which is empty.
Self executing functions are typically used to encapsulate context and avoid name collusions. Any variable that you define inside the (function(){..})() are not global.
The following code:
var same_name = 1;
var myVar = (function() {
var same_name = 2;
console.log(same_name);
})();
console.log(same_name);
produces this output:
1
2
By using this syntax you avoid colliding with global variables declared elsewhere in you javascript code.
I am not sure what this is called, other than defining an anonymous function and immediately invoking it.
It is perfectly legal, because
Defining an anonymous function is legal.
Invoking it and assigning the return value is also legal.
The end result is that myVar = 1.
This is an anonymous function (also called lambda function) that is being executed immediately with its return value (1) being assigned to a variable (myVar). It's legal because the specification says it is. It is a very common feature in many languages.

Categories