ActionScript中的问题与原型和静态类型变量原型、变量、静态、类型

由网友(Cavalier)分享简介:我正在开发一个闪存(闪存9,AS3)将数据连接到服务器和发送/接收/解析上的JavaScript / HTML聊天。我有一个这样的结构:I'm developing a flash (Flash 9, AS3) to connect to a server and send/receive/parse data t...

我正在开发一个闪存(闪存9,AS3)将数据连接到服务器和发送/接收/解析上的JavaScript / HTML聊天。 我有一个这样的结构:

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:调用可能未定义   方法escapeHtml通过参考   静态类型的字符串。

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

如果我删除:字符串,这一切工作正常,但后来我不得不检查 STR 是一个字符串,如果它不是不确定的等等。

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.

我有这样的很多功能在我的code,其中许多是接收用户输入的数据,所以我认为删除:字符串和做许多检查在每一个功能是不是最好的方法。

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.

我怎样才能让这个吧?

推荐答案

然后,只需定义功能:

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

    return str;
}

在你的类。

和调用它:

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

:)

阅读全文

相关推荐

最新文章