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

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.

Related

Calling one function with another function variables javascript [duplicate]

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.

javascript named function call throwing error [duplicate]

This question already has answers here:
Why use named function expressions?
(5 answers)
What's the point of naming function expressions if you can't really reference them by the names you give them? [duplicate]
(5 answers)
Closed 5 years ago.
var boo= function foo(){
console.log("I am foo");
}
boo(); // output: I am foo
foo(); // output: Uncaught ReferenceError
I am little confused with javascript named function. Can any one please explain why in above code snippet the foo() function call is throwing the error. Thanks in advance
var boo= function foo(){
There is a clear difference between a function and function expression.
What you have is an expression resolved to a variable. The way you are expecting to work needs to be a function or a variable resolved by a function expression.
From MDN docs
Here is an example of an anonymous function expression (the name is
not used):
var myFunction = function() {
statements
}
It is also possible to provide a name inside the definition in order
to create a named function expression:
var myFunction = function namedFunction(){
statements
}
One of the benefit of creating a named function expression is that in
case we encounted an error, the stack trace will contain the name of
the function, making it easier to find the origin of the error.
As we can see, both example do not start with the function keyword.
Statements involving functions which do not start with function are
function expressions.

Does the way a JS function is defined affect it's "performance"? [duplicate]

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 7 years ago.
Is there ANY difference in the following two ways of defining a functions?
METHOD 1)
var printName = function(name){
return("Hi! My name is ",name)
}
VS
METHOD 2)
function printName(name){
return("Hi! My name is ",name)
}
and I mean ANY, I'm new at JS and want to lay down my understanding of functions and Objects before I advance as I feel these 2 features are the 2 I'll use the most.
Yes there is a difference, but none that would affect the performance of the function code when it's called.
The difference has to do with when the function is created, but the performance is identical. Using your examples:
printName_1("Drew"); // This will fail, as printName_1 is not defined (yet)
printName_2("user4820485"); // This will work
var printName_1 = function(name){
return "Hi! My name is "+name;
}
function printName_2(name){
return "Hi! My name is "+name;
}
Functions that are declared using the latter syntax are initialized at the beginning of the block where they appear, so it looks like they can be called before they are defined.

Declare such Function if it does not exist [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
if function does not exist write function - javascript
I have a situation when some function X is being called. After some postbacks this function is no longer declared, but still being called by the code, obviously i get js error saying X is not defined . (call it a bug if you wish) but
It is not under my control to not call it or to change the calling functionality.
What I would like to do is a fail safe that will declare such function if it does not exist. So the logic is:
If function not declared then declare one.
Is that possible in javascript i.e. to declare/register a function dynamically in global scope?
Thanks.
if (typeof window.functionX === 'undefined') {
window.functionX = function() {
// fallback code here
}
}
Sure it is
if(!myFunc) {
myFunc = function() {}
}

Why are anonymous functions treated differently from named functions here? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Javascript: var functionName = function() {} vs function functionName() {}
What is the difference between a function expression vs declaration in Javascript?
Today I stumbled upon the following phenomenon:
foo();
bar();
function foo()
{
console.log("inside foo");
}
var bar = function()
{
console.log("inside bar");
}
FireBug complains with the following error message:
bar is not a function
Several tutorials claim that function f() and var f = function() are basically the same thing. Evidently, they are not, but what exactly is going on here?
Function declarations are available anywhere in the scope they're defined in, even before their physical definitions.
var bar = function() { ... }; is a normal variable that happens to hold a function. Like all other variables, it can only be used after its assigned.
(You cannot observe the future value of a variable)

Categories

Resources