将 replaceAll 方法添加到 ActionScript String 类 [英] Adding the replaceAll method to the ActionScript String class

查看:27
本文介绍了将 replaceAll 方法添加到 ActionScript String 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助来将替换所有功能添加到我的 Flex 项目中.我更愿意以尽可能自然的方式进行.

我想要实现的是能够运行此代码(使用 Flex 编译器)"aabbaaba".replaceAll("b","c") 并获得 "aaccaaca".我还想链接 replaceAll 调用.
注意:我实际上不会将 bs 替换为 cs,而是在编码时不知道的各种字符串!

我不想要的:
1. 使用带有全局标志的正则表达式.要替换的标记由广告运行时间决定,将它们转换为正则表达式并非易事.
2. 使用StringUtil.replace 方法.这是一个静态方法,链接很丑陋.
3.链splitjoin.因为在阅读代码的时候可能会让其他人感到困惑.
4. 禁用严格类型检查.我想对我的其余代码进行类型检查.

<小时>

这是我目前所拥有的:

String.prototype.replaceAll = function(replace:String, replaceWith:String):String{返回 this.split(replace).join(replaceWith);}

并且可以这样调用:

"aababacaaccb"["replaceAll"]("b", "c")["replaceAll"]("c", "a");

<小时>

另外,是否有反对通过原型扩展对象的建议?我也将接受一个反对通过原型扩展 String 对象的有力论据.

谢谢,
阿林

解决方案

我认为您已经获得了所有可能的技术答案.我将详细说明我认为处理这种语言的最佳方式.

不建议在诸如 AS3 之类的 OOP 语言中使用原型(主要是因为它们无视封装).你暗示你不想要一些让别人困惑"的东西(关于 split.join);好吧,AS3 中的原型非常混乱.举个例子,原型声明可以在代码的任何地方完成,因此它应该驻留在什么地方并不明显.如果其他人"在您的代码中遇到foo".replaceAll(),那么在何处可以找到该方法并检查它的真正作用完全不明显.

一个静态函数修正了这一点,而且非常简单.当然,你需要一个额外的参数,而且你不能真正正确地链接,但这是一件坏事吗?

如果您需要性能,split.join 是您的最佳选择.我敢打赌,更多的 AS3 开发人员了解 split.join 而不是使用原型.

另一方面,我认为最语义化和务实的方法是使用自己的语言方法(因此是我之前的回答).您正在尝试用 AS3 中的另一个字符串替换一个字符串中的所有针,为此该语言具有带有全局标志的 String::replace 方法.我很确定有一种方法可以轻松解析和使用正则表达式中的任何字符串.

我同意在某些情况下可能需要辅助方法(如 replaceAll),但我强烈建议您不要使用原型,而是使用更标准的方法,如 StringUtil.replace.

I need some help in adding the replace all functionality into my Flex project. I would prefer doing this in a way that is as natural as possible.

What I want to achieve is to be able to run this code (with the Flex compiler) "aabbaaba".replaceAll("b","c") and get "aaccaaca". Also I want to chain replaceAll calls.
Note: I won't be actually replacing bs with cs, but various string that will not be known at coding time!

What I don't want:
1. Use regular expressions with the global flag. The tokens to replace are determined ad run time and transforming them into a regular expression isn't straight forward.
2. Use the StringUtil.replace method. It's a static method and chaining is ugly.
3. Chain split and join. Because it may be confusing for others when reading the code.
4. Disable strict type checking. I want to have type checking for the rest of my code.


Here is what I have so far:

String.prototype.replaceAll = function(replace:String, replaceWith:String):String{
    return this.split(replace).join(replaceWith);
}

And can be called like this:

"aababacaaccb"["replaceAll"]("b", "c")["replaceAll"]("c", "a");


Also, are there any recommendations against extending objects through the prototype? I will also accept an answer that has strong arguments against extending the String object through the prototype.

Thank you,
Alin

解决方案

I think you got all the technical answers possible. I will elaborate in what I think is the best way to approach this language-wise.

Prototypes are not recommended in an OOP language such as AS3 (mostly because they defy encapsulation). You imply that you don't want something "confusing to others" (in relation to split.join); well, prototypes in AS3 are very confusing. Just as an example of this, a prototype declaration can be done from anywhere in your code, and as such is not obvious where it should reside. If "others" encounter "foo".replaceAll() in your code, it's not at all obvious where one can find that method and check what it really does.

A static function amends this and is pretty straight forward. Sure, you need an extra argument, and you can't really chain properly, but is this such a bad thing?

If you need performance, split.join is the way to go. I would bet good money that more AS3 devs know about split.join than the use of prototypes.

On the other hand, I think the most semantic and pragmatic way would be to use the own language method (hence my previous answer). You are trying to replace all needles in a string with another string in AS3, and for that the language has the String::replace method with the global flag. I'm pretty sure there is a way to easily parse and use any string in regexp.

I concur that in some cases a helper method (like replaceAll) could be necessary, but I would strongly encourage you to not use prototypes, and instead use a more standard way, like StringUtil.replace.

这篇关于将 replaceAll 方法添加到 ActionScript String 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆