Javascript extends and function overloading

Given this code I would never expect to print “Letter”:

class Letter {
	
	function PrintLetter() {
		Debug.Log( "Letter");
	}
}

class LetterA extends Letter {
	
	function PrintLetter() {
		Debug.Log( "LetterA");
	}
}

class LetterB extends Letter {
	
	function PrintLetter() {
		Debug.Log( "LetterB");
	}
}

private var letterA : Letter = LetterA();
private var letterB : Letter = LetterB();

private var anyLetter : Letter;
private var anyObject;

function Start () {

	anyLetter = letterA;
	anyObject = letterA;
	
	letterA.PrintLetter();
	letterB.PrintLetter();
	anyLetter.PrintLetter();
	anyObject.PrintLetter();
}

The output is:

  • Letter
  • Letter
  • Letter
  • LetterA

The only correct answer is the last once which doesn’t work with #pragma strict, thus not working on the iPhone.

Am I missing something ?

I don’t know much about Javascript, but do you need to declare the base method as virtual so it is overridden by the derived class’s definition?

class Letter {

virtual function PrintLetter() {
Debug.Log( “Letter”);
}
}