简体   繁体   English

ActionScript3-扩充String的原型

[英]ActionScript3 - augmenting prototype of String

In AS2 I could do the following: 在AS2中,我可以执行以下操作:

String.prototype.startsWith = function(s){
     return this.indexOf(s) == 1
}

thus, startsWith is available on every String object 因此, startsWith在每个String对象上都可用

var s = "some string to test with";
s.startsWith("some") // returns true

and did it with great repository of cool tools: 并通过很棒的工具库完成了它:

var s = "some @VAR string";
s.startsWith("some");//returns true
s.endsWith("ing");//returns true
s.contains("@");//returns true
s.dataBind({VAR: "awsome"})// returns 'some awsome string'
s = "b";
s.isAnyOf("a","b","c"); //true, because "b" is one of the options
s.isInArr(["a","b","c"]); //true, because "b" is in the passed array
var o = { foo: function(i) { return "wind " + i } }
s = "foo";
f.call(o,3) //returns "wind 3" because method foo is ivoked on o with 3
f.apply(o,[3]) //returns "wind 3" because method foo is ivoked on o with 3
var a1 = [], a2 = []
s.push(a1,a2) // pushes s into a1 and a2

And so on, and so forth with many cool things that makes coding much more fun (and blazing fast when smartly used) 等等,还有很多有趣的事情,这些事情使编码变得更加有趣(并且在聪明地使用时快速燃烧)

It's not just about String, I have such utils for Number, Date, Boolean, and so on. 这不仅是关于String的,我还有用于Number,Date,Boolean等的utils。

Here's what I tried: 这是我尝试过的:

[Test]
public function test_stringPrototype()
{
    String.prototype.startsWith = function(s):Boolean
    {
        return return this.indexOf(s) == 1;
    }

    assertTrue( !!String.prototype.startsWith ) //and so far - so good ! this line passes

    var s:String = "some str";
    assertTrue(!!o.startsWith ) //and this won't even compile... :(
}

And this won't even compile, not to mention pass or fail the test... error: Access of possibly undefined property startsWith through a reference with static type String. 而且它甚至不会编译,更不用说通过或通过测试了……错误: Access of possibly undefined property startsWith through a reference with static type String.

Whats the way to do it in AS3? 在AS3中怎么做?

you could always have the utility class that will collect all those methods and work on the string, eg 您始终可以拥有实用程序类,该实用程序类将收集所有这些方法并在字符串上工作,例如

package utils
{
    public class StringUtils
    {
        public static function startsWith(input:String, test:String):Boolean
        {
            return input.indexOf(test) == 0;
        }
    }
}

usage: 用法:

trace(StringUtils.startWith("My string", "My"));

or as a "global" function: 或作为“全局”功能:

package utils
{
    public function startsWith(input:String, test:String):Boolean
    {
        return input.indexOf(test) == 0;
    }
}

usage: 用法:

trace(startWith("My string", "My"));

best regards 最好的祝福

Yes of course, use string representation of a variable name: "startsWith"; 是的,当然,使用变量名的字符串表示形式:“ startsWith”; Example down 下来的例子

        String.prototype.traceME = function():void
        {
            trace(this);
        }

        var s:String = "some str";
        s["traceME"]();

else method: 其他方法:

            var s:Object = new String("some str");
            s.traceME();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM