actionscriptactionscript-2mtasc

can't add a new function to String class via prototype in as2


following code;

String.prototype.myFunction = function() { trace("my function is called"); };
var myString:String = "myString";
myString.myFunction();

causes this error with mtasc compiler:

type error String has no field myFunction

it must be possible to add new functions to a class via prototype.

is there any configuration i can do for mtasc to be able to compile this code?


Solution

  • problem was specifying type information at myString variable definition.

    it is compilable and working in that case:

    String.prototype.myFunction = function() { trace("my function is called"); };
    var myString = "myString";
    myString.myFunction();