Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Javascript Delegates!

Discussion in 'Scripting' started by bronxbomber92, Mar 23, 2008.

  1. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    Today on IRC Neil was explaining delegates in C# to a fellow Unity user, and that got me wondering, "Does Javascript support delegates?". I asked Neil, and he didn't really know but suggested to just mess around and see for myself, so I did! It turns out that Javascript does support delegates. Only downfall is that you can't declare any delegate types, and that there is no compile time checks, so if anything goes wrong you'll get an exception at run-time.

    So, here's my example I used to find out if JS had delegates.
    Code (csharp):
    1. var func;
    2.  
    3. function Start()
    4. {
    5.     func = PrintSomeThing;
    6.     if( func )
    7.         func("Calling a delegate!");
    8.     else
    9.         print("didn't work");
    10. }
    11.  
    12. function PrintSomeThing(str)
    13. {
    14.     print(str);
    15. }
     
  2. jmpp

    jmpp

    Joined:
    Jun 1, 2010
    Posts:
    93
unityunity