Is there a reason for allowing such deviant syntax?
I often wonder that about a lot of JS :)
function a() {
function foo() { return "All is well with the world..." }
return foo();
function foo() { return "Oh God no. This is horrible!" }
}
function b() {
var foo = function() { return "All is well with the world..." }
return foo();
foo = function() { return "Oh God no. This is horrible!" }
}
a(); // "Oh God no. This is horrible!"
b(); // "All is well with the world..."
I think it was a side-effect of turning it into Bytecode first as @allObjects says, but then they thought it was handy so they left it in...
At some point I will implement this properly, there are some nice side-effects when applied to Espruino:
function a() {
function b() {
// ...
}
setTimeout(b,500);
}
The code above would have had the function b parsed beforehand, so execution would be faster, and if you called it twice it'd be able to use the same copy of b's code, saving memory.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
I often wonder that about a lot of JS :)
I think it was a side-effect of turning it into Bytecode first as @allObjects says, but then they thought it was handy so they left it in...
At some point I will implement this properly, there are some nice side-effects when applied to Espruino:
The code above would have had the function b parsed beforehand, so execution would be faster, and if you called it twice it'd be able to use the same copy of
b
's code, saving memory.