Calling a class instance

Passing an instance method to another object to call.

I want to pass to an object "A" that already exists the address of an instance of another object "B" that already exists.

  A.setObj= function ( obj )
      this.obj = obj
  end

When I want to refer to object B in some method of object "a", I simply write:

    this.obj.name_method()

This works, but it forces me to permanently write down in the code which method will be called.

In Delphi/FreePascal it is possible to pass the address of a method and an instance of a given object - so you do not have to write permanently in the code which method you need to call ("type procedure of object").

However, this doesn't work in MicroScript2 and I don't know how to pass the instance and what method to call.

When I pass the method address and call this method (see this.call() in the example), it turns out that the correct "TestClass" cluster method has been called, but the "this" field contains a reference to the "TimerCall" object.

Now when I call this.call() >> "TestClass" >> all references to variables and functions will refer to the "TimerCall" object.

The code of one object will use the data of the other object thinking it is its data.

If both objects have the same field names to which the code from the called method refers, it will work.

It will not report an error. Only operations will be performed on data that is not your own.

If the objects have different field names, it will report an error and you will not know why it reports an error that a given variable or function does not exist.

Code demonstrating the error >> https://microstudio.io/i/Loginus/callme2/

This problem can be solved

  1. by adding a new data type to MicroScript, such as an instance.
  2. add a function that will return the name of a given function.

name = A.functionName.getName() and there would be a cancellation at the facility this.obj[ name ]()

This code now works properly (name is a string variable).