userTester
asked on
Function expression over declaration
Hi there,
I am learning about JavaScript functions, and would like to understand why the compiler chooses to return the value for the function expression over the function declaration?
// Function declaration
function myFunction() {
return 'Hi';
}
// Function expression
var myFunction = function myFunction() {
return 'Hello';
};
console.log(myFunction()); // returns Hello
Thanks!
I am learning about JavaScript functions, and would like to understand why the compiler chooses to return the value for the function expression over the function declaration?
// Function declaration
function myFunction() {
return 'Hi';
}
// Function expression
var myFunction = function myFunction() {
return 'Hello';
};
console.log(myFunction());
Thanks!
Because you are essentially re-declaring the variable myFunction. If you were to reverse the order, the function declaration would take precedence, since it would execute later.
ASKER
Steve
Thanks for responding. I reversed the order, but the function expression still gets precedence - prints Hello.
Thanks for responding. I reversed the order, but the function expression still gets precedence - prints Hello.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Slick812,
According to javascriptissexy, you are correct, except perhaps for the "applied" part.
So, as you infer, it looks like the function expression variable which has the same name as the function declaration, overwrites the function declaration during hoisting.
This would mean that any variable with the same name as a function, would overwrite any function of the same name, if they're in the same scope of course??
functions are read and applied before the variables are read
According to javascriptissexy, you are correct, except perhaps for the "applied" part.
So, as you infer, it looks like the function expression variable which has the same name as the function declaration, overwrites the function declaration during hoisting.
This would mean that any variable with the same name as a function, would overwrite any function of the same name, if they're in the same scope of course??
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I've requested that this question be closed as follows:
Accepted answer: 250 points for Slick812's comment #a40828073
Assisted answer: 0 points for userTester's comment #a40828143
Assisted answer: 250 points for Slick812's comment #a40828244
for the following reason:
Thanks Slick812!
Accepted answer: 250 points for Slick812's comment #a40828073
Assisted answer: 0 points for userTester's comment #a40828143
Assisted answer: 250 points for Slick812's comment #a40828244
for the following reason:
Thanks Slick812!
ASKER
I need to make a correction about my post: ID: 40828143.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
????, it is not correct or incorrect, because, javascript is just a scripting language, and in it Nothing gets complied so you say - "when the compiler" does not apply. . An I have never heard of using the word "hoisting" in programming, as if something is being lifted up? In order to have a function work, the script TEXT in the function definition MUST be read and interpreted to a browser sub-processes "ordering" of operations to come close to the text intention of the writer of the code. So the functions must be setup, BEFORE the code line execution progression, begins at the top and goes line by line down to the bottom.
Do not ever think that the TEXT you write for programming language syntax, has any thing at all to do with, or correspond to any actual machine CPU memory manipulations that produces real light pixel output on your monitor. You program in just TEXT, and it has NO operational value, until it is interpreted and transformed to some computer (not human) electronic operations, which have no relation to any text like -
for (i=0; i<7;++i)
Do not ever think that the TEXT you write for programming language syntax, has any thing at all to do with, or correspond to any actual machine CPU memory manipulations that produces real light pixel output on your monitor. You program in just TEXT, and it has NO operational value, until it is interpreted and transformed to some computer (not human) electronic operations, which have no relation to any text like -
for (i=0; i<7;++i)
ASKER
Thanks Slick812,
I think there is a move toward compiled code, but I am not sure about the technicalities, and whether it is just some form of compilation, and not the real deal. Apparently, Chrome was the first browser with an optimizing engine that compiles JavaScript into native code. Also, many new books on JavaScript, speak in terms of compilation, compile time, and a compiler, so something's brewing.
Hoisting is also a term being used in many newer JavaScript books and articles. I guess it's just a word being used for the same process you've explained.
I did some very basic JavaScript many years ago, and I too never came across these terms, and essentially how JavaScript is being used today.
I think there is a move toward compiled code, but I am not sure about the technicalities, and whether it is just some form of compilation, and not the real deal. Apparently, Chrome was the first browser with an optimizing engine that compiles JavaScript into native code. Also, many new books on JavaScript, speak in terms of compilation, compile time, and a compiler, so something's brewing.
Hoisting is also a term being used in many newer JavaScript books and articles. I guess it's just a word being used for the same process you've explained.
I did some very basic JavaScript many years ago, and I too never came across these terms, and essentially how JavaScript is being used today.
ASKER
Thanks Slick812!