Why this simple code does not work?

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:

  1. In the error message says that some variable ā€œvā€ is undefined. But the code does not have the variable ā€œvā€.
  2. 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. :slight_smile:

1 Like