In Unityscript, a sort of "class wrapper" ...

Here’s a good one for you language mechanic geniuses!

Say you have a class

static class handy
 {
 function aa ( . various args . ) { }
 function bb ( . various args . ) { }
 function cc ( . various args . ) { }
 }

Of course, you can call those functions from anywhere in your project as

handy.aa( this, that, theOtherThing.x );
handy.bb();
handy.cc( galacticSpin );

and so on. Now imagine you have another class,

static class handyPLUS
  {
  }

the idea is that handyPLUS has all, and exactly the same, functions as handy - but - one extra line of code.

So essentially, you could achieve this manually, by copying and pasting:

static class handyPLUS
 {
 function aa ( . paste from 'handy aa' . )
   {
   handy.aa( same );
   one more line of code;
   }
 function bb ( . paste from 'handy bb' . )
   {
   handy.abb same );
   one more line of code;
   }
 function cc ( . paste from 'handy cc' . )
   {
   handy.cc( same );
   one more line of code;
   }
 }

Of course, that would be lame. What’s the proper way to do this in Unityscript? Cheers !

My attempt at solving this is the following construct:

//The Basic Class//  

#pragma strict

public class Helper {
	
	public var addedCode = function(){};

	function aa() {
		replace();
		Debug.Log("aa");
		addedCode();
	}
	
	function bb() {
		replace();
		Debug.Log("bb");
		addedCode();
	}
	
	function replace(){}
}

//The Extended Class//
#pragma strict

public class HelperPLUS extends Helper{

	//a Singleton pattern
	private static var helperInstance = new HelperPLUS();
	public static function Instance(){ 
		if(helperInstance == null ) helperInstance = new HelperPLUS(); 
		return helperInstance;
	}
	
	override function replace(){
		addedCode = function(){
			//add the extra code here.
			Debug.Log("extended");
		};
	}
}

Calling the HelperPLUS methods is as always with singletons: HelperPLUS.Instance().aa()
I don’t know if this is what they call UnityScript or if it passes as real javascript, but it works on my editor.

Okay so this is c# - so if you need the answer to be exactly in unityscript I will convert this to a comment.

using UnityEngine;
using System.Collections;

public class Handy : HandyHelpers.HandyBase<HandyHelpers.HandyDefaultExtension>
{
}

public class HandyPlus : HandyHelpers.HandyBase<HandyHelpers.HandyPlusExtension> 
{
}

namespace HandyHelpers
{
	public class HandyBase<T> where T : IExtension, new()
	{
		protected static T mExtension = new T();
		
		public static void aa( /* Params */ )
		{
			// Do some stuff
			Debug.Log("aa");
			mExtension.DoExtension();
		}
		
		public static void bb( /* Params */ )
		{
			// Do some stuff
			Debug.Log("bb");
			mExtension.DoExtension();
		}
		
		public static void cc( /* Params */ )
		{
			// Do some stuff
			Debug.Log("cc");
			mExtension.DoExtension();
		}
	}
	
	public interface IExtension
	{
		void DoExtension();
	}
	
	public class HandyDefaultExtension : IExtension
	{
		public void DoExtension()
		{
		}
	}
	
	public class HandyPlusExtension : IExtension
	{
		public void DoExtension()
		{
			Debug.Log("Extension");
		}
	}
}

The namespace HandyHelpers is there to basically hide all the gruesome base classes. Then any line of code you want to execute after all functions can simply be slotted into DoExtension allowing you to easily extend handy into whatever you want.

So currently:

Handy.aa(); // Produces "aa"
Handy.bb(); // Produces "bb"
Handy.cc(); // Produces "cc"
HandyPlus.aa(); // Produces "aa" "Extension"
HandyPlus.bb(); // Produces "bb" "Extension"
HandyPlus.cc(); // Produces "cc" "Extension"

Not sure if that is any better but at least once the initial work is done extending it is pretty easy.