Skip to main content

Enactor Script Modifiers

this, super, and global are scope modifiers in Enactor Script - references to script contexts that behave like objects. Between them they let a method, and the methods nested inside it, share and inherit variables and behaviour dynamically.

'this', 'super', and 'global'

As covered in Enactor Script Methods, super refers to a method's parent scope - the scope the method was defined in. this refers to the current method's own scope. Because of this, any method scope (and indeed the top-level global scope) can be treated as an object context - collectively, this, super, and global are referred to as 'this' type references.

Printing a this reference shows which scope it belongs to and what its parent is. For example, from the global scope:

print( this );
// 'this' reference to object: global

And from inside a nested method:

foo() { print(this); print(super); }
foo();
// 'this' reference to object: foo
// 'this' reference to object: global

This shows that foo()'s own this is local (named foo), and its parent - reached through super - is the global scope, which is exactly the scope foo() was defined in.

global

global always refers to the top-most scope, regardless of how deeply nested the current method is:

global.foo = 42;

Global variables are not special in themselves - they are only "global" because they live in the topmost scope. If you'd rather not pollute the global scope, you can create a dedicated object to hold shared state instead, using the object() command:

// Create a top level object to hold some state
dataholder = object();

foo() {
...
bar() {
dataholder.value = 42;
}

bar();
print( dataholder.value );
}

object() creates an empty scripted object context. dataholder is a this type reference just like any other scripted object scope, so it can hold variables and be passed around freely.

Synchronized Methods Revisited

Synchronized methods synchronize on their common super reference, which is what makes two synchronized methods in the same scope behave as if they belonged to a single class. All of the following synchronize on the same underlying object:

print( this ); // 'this' reference to object: global

// The following cases all synchronize on the same lock
synchronized ( this ) { } // synchronized block
synchronized int foo () { } // synchronized method foo()
synchronized int bar () { } // synchronized method bar()
int gee() {
synchronized( super ) { } // synchronized block inside gee()
}