Calling one function with another function variables javascript [duplicate] - javascript

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Learning JavaScript: Lexical Versus Dynamic Scoping
(3 answers)
What is lexical scope?
(21 answers)
Closed 8 months ago.
I am trying to learn JS internals.I am currently trying to print message "yo" both times using the two console.logs. I tried using call,bind or apply to see if it will help but it still doesn't. On the execution stack it places func2 execution context above the func1 execution context. So, I was hoping it would take the func2.
var a = 'hello';
function func1() {
console.log(a);
}
function func2() {
var a = 'yo';
func1();
console.log(a);
}
func2();
I know that if we put it as a nested function it will work.
var a = 'hello';
function func2() {
var a = 'yo';
function func1() {
console.log(a);
}
func1();
console.log(a);
}
func2();
Is there a way to print the internal message without declaring a function inside like the 2nd example without passing a variable to func1?
I think the reason why the first is not working is because this refers to the window but slightly confused due to Execution Context placed over the Execution Stack.

Related

Javascript - Defining a local variable after assigning a new value in a already declared global variable is overriding the global variable. Why? [duplicate]

This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Closed 1 year ago.
I am learning javascript global variable and local variable. When I did experiment with it (see jsfiddle below) then I am confuse that why after executing myFunction1() the value of variable a became 4 instead of 5. What do you think happened inside the javascript memory which made its value 4 instead of 5 ?
var a = 4;
myFunction1();
function myFunction1() {
a= 5;
var a= 6;
}
alert(a); //a = 4
myFunction2();
function myFunction2() {
a= 5;
}
alert(a); //a = 5
You've just discovered hoisting.
By declaring var a inside the function, it is available to the whole function context (even before its declaration), so when you write a = 5 the actual a is not the one you expect, it's the function-scoped a.
In the second case, a is the one you expect (outside the function)

Javascript variable scope in anonymous function [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 3 years ago.
I'm confused as to why the below will output "1".
If I use var to declare a in myFunc that should limit the scope of a to that function.
Calling "a" in the anonymous function (in the setInterval call) is a different function so why is that considered with the scope of myFunc?
Is it only because the function is anonymous? I would expect a to be available to another function unless bind() was used.
myFunc = function(){
var a = 1;
var int = setInterval(function () {
console.log(a);
}, 5);
}
The function bound to setInterval is a closure, when something is unknown in its own scope, it looks in the outer scope, in your case myFunc scope

Function declaration inside conditional [duplicate]

This question already has an answer here:
Why JavaScript function declaration behave differently in chrome and safari? [duplicate]
(1 answer)
Closed 5 years ago.
In a book You Don't Know JS: Scope & Closures There is this sample of code I don't understand fully.
"Function declarations that appear inside of normal blocks typically hoist to the enclosing scope, rather than being conditional as this code implies:"
foo(); // "b"
var a = true;
if (a) {
function foo() { console.log( "a" ); }
}
else {
function foo() { console.log( "b" ); }
}
What does it mean? How is it even possible? Is the conditional not working?
It's happening because function declarations are moved to the top of the file by the javascript parser. That's what they mean by hoisting. The last declaration of foo overwrites the first when they are hoisted.

Can you explain this weird function declaration behavior? [duplicate]

This question already has an answer here:
Function call javascript [duplicate]
(1 answer)
Closed 6 years ago.
Can you explain this?
var guessWhat = function(){ console.log('Print this!!!'); };
function guessWhat(){ console.log('Print that???'); }
guessWhat();
// output: Print this!!!
Both are declared on the global scope. Why is the the second line not overriding the first? Is the second function lost in limbo?
function guessWhat(){ console.log('Print that???'); } // declaration
This is a function declaration, it is defined before any code is executed.
var guessWhat = function(){ console.log('Print this!!!'); }; // literal
This is a function literal, it is defined at run-time.
so, the function definition gets loaded first (before any code), and the function literal afterwards, which overrides the first definition, hence this behaviour.
Read more here.

What is the point of using a named function expression? [duplicate]

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Why use named function expressions?
(5 answers)
Closed 9 years ago.
I'm going through this blog about the difference between function declarations and function expressions.
It gives these two examples. They call the first an "anonymous function expression" and the second a "named function expression."
// anonymous function expression
var a = function(){
return 3;
}
// named function expression
var b = function bar(){
return 3;
}
I tested these two in Chrome's JS console and I see the following:
a()
=> 3
b()
=> 3
bar()
=> bar is not defined
My question is: In the second function expression declaration, what is the point of "bar"? In general, why does one ever use a named function expression?
Some people prefer to do it like this because if errors occur, your functions have names. It's mostly a matter of preference and how often you have trouble with unnamed functions.
You don't normally see it used in a var declaration, but instead when declaring callbacks:
callbackFunction(function success() { ... }, function fail() { ... })
That way you know which argument is which, they're labelled, and if one of them fails you get a precise indication of which one broke.
var b = function bar(){
return 3;
}
bar()
=> bar is not defined
The identifier bar is only available inside of the function. Try
var b = function bar() {
console.log(bar);
}
b();
why does one ever use a named function expression?
To allow referencing a function expression that was not assigned to a reachable or constant variable, e.g. for recursion in an IEFE.
Also, named functions show up different during debugging, e.g. in call stack (trace)s or breakpoint listings. Often you can use a (named) function declaration instead of a function expression, see also http://blog.niftysnippets.org/2010/03/anonymouses-anonymous.html.

Categories

Resources