why this simple code does not work?
It doesnāt work because you didnāt define test1()
. The line test1()
is not simple. It is to be working if there is some definition of it.
I donāt understand. ātest1()ā function is defined immediately after the ātest()ā function.
But it is doesnāt work
Test 1 needs to be defined before test() runs. (aka test() needs to be put after test1())
You didnāt run the function, either.
actually I ran the function at line 1
@Seojin_Roy_Lee test1() is called upon within test()
Oh. (I hate 20 char)
In the code below function ātest1()ā is still after the function ātest()ā, but code works fine.
Just function ātest()ā is called after all definitions.
function test()
{
test1();
}
function test1()
{
hero.say('ok');
}
test();
I donāt understand, why?
Is it a game bug or feature?
switch the order of the functions.
You have to define a function before you can call it.
It works in the second format because you call on test() after defining it. In the first version you tried to call a function that doesnāt exist yet because you hadnāt defined it yet.
Not correct, because the following code works, although the function is defined after the call
test();
function test() {
hero.say("It works");
}
Youāre right, but I prefer another style of programming. When the function declarations are at the bottom.
Iām not asking how to write code, Iām indicate to a bug in the game.
Itās not a bug. You should search online for why this happens (Iām sure thereās an explanation)
Iām sorry, but itās a bug.
2 Facts:
- In the error message says that some variable āvā is undefined. But the code does not have the variable āvā.
- The following code works in any modern browser (and even in IE8):
<!-- Test.html-->
<script>
test();
function test()
{
test1();
}
function test1()
{
alert('ok');
}
</script>
Hey Gaulina,
Youāre absolutely right that itās a bug. Esper only does var hoisting mechanics for functions, which covers the use-case of functions referring to functions defined below them. However the function isnāt actually defined until execution reaches itās declaration.
Iāll work on getting that fixed up.