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): var func; function Start() { func = PrintSomeThing; if( func ) func("Calling a delegate!"); else print("didn't work"); } function PrintSomeThing(str) { print(str); }
Hey bronxbomber92! The other day I ran into the need of declaring delegate types in JS and was dismayed when reading this post of yours. Nevertheless, reading the documentation for Unity 3.x led me to an alternate and, at least as far as I can see, practically equivalent solution, which I posted as a response in another thread: http://forum.unity3d.com/threads/47765-delegates-function-pointers?p=604322&viewfull=1#post604322 Enjoy! - jmpp