Question: Why and how are a Script stores in a Handler works?

Greetings from Germany. My English is not very good, I try to explain my Question.

Look here:

set avalue to 3
my testhandler(avalue)

on testhandler(x)
	script e
		set x to x + 5
	end script
	run e
	return x
end testhandler

It works and the result is 8 (!) although inside script e is x not (!) defined. It comes from outside.

Why has the script-object e the Variable x?

set x to 3
#my testhandler(avalue)

script e
	set x to x + 5
end script
run e
return x

The same Construct without a Wrapper-Handler: Failed – x is not defined.

Thanks for Answers.

These Constructs are needed for a redundancy coding.

Hi.

The script object e is local to the handler and is “instantiated” (ie. created) every time the handler’s executed. The process creating it already knows about x as it’s the handler’s parameter variable. Basically, within the scope of the handler, x is x, even inside a local script object.

But if you were to give e its own x, it would use that instead. (I see KniazidisR’s added something similar.):

set avalue to 3
my testhandler(avalue) --> 3

on testhandler(x)
	script e
		property x : 15
		set x to x + 5
	end script
	run e
	return x
end testhandler

I deleted my wrong explanation. As I see now, declaring x of inside script object as local, property, or global makes the same. It is enough some declaration and the variable became encapsulated. It was new for me too.