View Single Post
Old 07-21-2021, 09:37 PM   #4
SaulT
Human being with feelings
 
Join Date: Oct 2013
Location: Seattle, WA
Posts: 876
Default

"This" refers to the current object. If it helps, the following two functions have the same effect

function foo(x) instance(a) ( a = x; );
function foo(x) ( this.a = x; );

Ok, so going back to scope...

It's a form of object-oriented programming. Functions operate on a certain scope. If not attached to a specific object, the scope will be global, otherwise they will search the object's scope first before moving up to search globally.


function foo(x) instance(a,b,c) ( a = x; );

This tells the code executed within foo() that a b and c are all variables that are attached to the current object. If a variable is called within foo() that is not defined by instance() then foo() will look globally.

bar.foo(5);

Creates "bar.a" and sets it to 5.

foo(5);

Creates "a" and sets it to 5. Since no object is specified the scope is set to global.

bar1.foo(6);
bar2.foo(9);

bar1.a is now 6
bar2.a is now 9

If you look at the debug functions and look at the listed of variables created it might help illustrate the point, too.

Hope this helps a little.
SaulT is offline   Reply With Quote