In GScript, functions can act as objects. This is called "nameless" or "unnamed" function definition and is popular in ECMAScript-like languages such as JavaScript. This results in variable space also being populated with function names, so be careful to not name functions with already defined variable names; most likely, one or the other will be overwritten.

Consider:

temp.foo = function ()
{
  player.chat = "Gads";
};

temp.foo();

... would set the player's chat to "Gads".

Because of this you can also send functions as parameters:

temp.bar = function(temp.baz)
{
  temp.baz();
};

temp.bar(temp.foo);

... would set the player's chat to "Gads".

Helpful hint: Prefix your parameter variables all the time while using nameless functions. The following will not work:

temp.bar = function (baz)
{
  baz();
};

... because function (baz) is actually translated to function (temp.baz) whereas baz(); is not translated to temp.baz(); (instead it is trying to run a global function called baz() which probably doesn't exist, and if it does, it probably will give the wrong result anyway).
Comments [Hide comments/form]
Perhaps a "real-world" example of function prototyping could be added to this page? Just by reading it, I don't see any scenario in which it would be favorable to use.
-- DylanHenrich (2009-09-13 17:28:53)
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki