Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionScript problem with prototype and static type variables

I'm developing a flash (Flash 9, AS3) to connect to a server and send/receive/parse data to a chat on JavaScript/HTML. I have a structure like this:

package {
    public class myClass {
        String.prototype.escapeHtml = function() {
            var str = this.replace(/&/g, "&");
            str = str.replace(/</g, "&lt;");
            str = str.replace(/>/g, "&gt;");
            return str;
        }

        function writeToBrowser(str:String) {
            ExternalInterface.call("textWrite",str.escapeHtml());
        }
    }
}

When I compile it, I get this error:

1061: Call to a possibly undefined method escapeHtml through a reference with static type String.

If I remove the :String, it all works fine, but then I'd have to check if str is a String and if it's not undefined and so on.

I have many functions like this on my code, many of them receive user-entered data, so I think that removing the :String and doing many checks on every function isn't the best approach.

How can I make this right?

like image 305
Mauricio Avatar asked Dec 06 '25 06:12

Mauricio


2 Answers

Then just define the function:

public function escapeHtml( str : String ) : String
{
    var str = this.replace(/&/g, "&amp;");
    str = str.replace(/</g, "&lt;");
    str = str.replace(/>/g, "&gt;");

    return str;
}

in your class.

And call it:

public function writeToBrowser( str : String )
{
    ExternalInterface.call( "textWrite", escapeHtml( str ) );
}

:)

you get an error because the compiler is in strict mode. if you want to stay in strict mode you can try this:

ExternalInterface.call("textWrite",str["escapeHtml"]() );
like image 21
OXMO456 Avatar answered Dec 07 '25 23:12

OXMO456



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!