2011年2月25日 星期五

GWT-GWT.create() 限制

GWT.create() 不支援 class type 做為函式參數,再呼叫 GWT.create(clazz) 產生該類別實例的方式,需注意這個限制。以下範例在 GWT Development Mode 可執行,在 Comple 為 Javascript 時會有 Error。

public class MyFactory {

    public Foo makeFoo(Class<? extends Foo> clazz) {
        Foo ret = GWT.create(clazz);
        return ret;
    }
}

2011年2月19日 星期六

Msysgit/MinGW 32中文亂碼與輸入問題

解決中文顯示問題

修改 C:\Program Files (x86)\Git\etc\profile 加入以下設定

alias ls='/bin/ls --show-control-chars -F --color=tty'
解決中文輸入問題

修改 C:\Program Files (x86)\Git\etc\inputrc 加入以下設定

# disable/enable 8bit input
set meta-flag on
set input-meta on
set output-meta on
set convert-meta off

2011年2月12日 星期六

實作 Javascript String trim(), startsWith() 與 endWith()

String 類別是最常用的類別,但在 javascript 中並非提供基本函式,像是 substr(), trim(), startsWith() 等,這篇文章示範如何實作 Javascript String 類別的 buildin 函式

     myVar = substr2(myVar, x, y); //Your function
    myVar = myVar.substr(x, y); //built-in function

Trim()

    String.prototype.trim = function(){return 
    (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))}

startsWith()

    String.prototype.startsWith = function(str) 
    {return (this.match("^"+str)==str)}

endsWith()

    String.prototype.startsWith = function(str) 
    {return (this.match("^"+str)==str)}

接著就可以用 build-in 的方式,呼叫這幾個函式

    var myStr = "Earth is a beautiful planet";
    var myStr2 = myStr.trim();  
    //=="Earth is a beautiful planet";
  
    if (myStr2.startsWith("Earth")) // returns TRUE
  
    if (myStr2.endsWith("planet")) // returns TRUE
  
    if (myStr.startsWith("Earth")) 
    // returns FALSE due to the leading spaces…
      
    if (myStr.endsWith("planet")) 
    // returns FALSE due to trailing spaces…