2009年9月24日 星期四

[JavaScript] Chapter 6&7 Notes

Reference: JavaScript - The Definitive Guide Chapter 6, Chapter 7
  • for/in loop: (section 6.9)
    1. 可以動態改變指定的 variable,例如下面這段程式碼複製 obj 的所有屬性名稱到 arr 中。
        var obj = {x:1, y:2, z:3};
        var arr = new Array( );
        var i = 0;
        for(arr[i++] in obj) /* empty loop body */;
      
    2. for/in 存取 property 並沒有依照某個順序,完全是 implementation-dependent,所以在 loop 中新增/刪除某個 property 要好好考慮。
  • with 指令會暫時改變 scope chain,雖然程式碼比較好寫,但執行效率會變差,盡量不要用。(section 6.18)

  • 當 object 用 obj["prop"] 來存取時,"prop"可以做 string 的操作,例如 obj["prop"+i] 動態決定要存取什麼屬性。如果用 obj.prop 的寫法,則 prop 當然是寫死的文字。這就是兩種 object 存取方式的差別。(section 7.3)

  • 所有自訂的 object 屬性都是 enumerable 的 (ie. 可使用 for/in loop 存取);Nonenumerable 屬性指的是繼承 Object class 來的那些屬性。所以 propertyIsEnumerable()(某自訂屬性是否 enumerable) 得到的結果幾乎會跟 hasOwnProperty() (是否有某個自訂屬性)一樣。(section 7.4)
  • (... 閱讀「[JavaScript] Chapter 6&7 Notes」全文)

    2009年9月23日 星期三

    [JavaScript] try/catch/finally

    Reference: JavaScript - The Definitive Guide, Section 6.17

    try/catch/finally 的執行示意圖:(四種情況)



    (... 閱讀「[JavaScript] try/catch/finally」全文)

    2009年9月17日 星期四

    [JavaScript] Variable Scope

    Reference: JavaScript - The Definitive Guide, Section 4.3

    function 內宣告的 var scope 是整個 function 的,舉例如下:
    var scope = "global";
    function f( ) {
      alert(scope);   // Displays "undefined", not "global"
      var scope = "local";  // Variable initialized here, but defined everywhere
      alert(scope);  // Displays "local"
    }
    f( );
    
    第一個 alert(scope); 會 undefine 是因為下面那一行宣告的關係,
    就是說,這時候下面那行的 scope 已經宣告了,只是還沒初始。
    (... 閱讀「[JavaScript] Variable Scope」全文)

    2009年8月27日 星期四

    Selenium Docs (1) - 基本介紹

    Selenium 介紹
    Selenium 包含三大部分:(1) Selenium-IDE;(2) Selenium-RC;(3) Selenium-Grid。
    • Selenium-IDE:Firefox 的附加元件,可以錄下操作網頁的動作作為 test cases,也可以自己編輯這些 test cases。
    • Selenium-RC:讓會寫程式的人利用現有的程式語言做自動化測試,selenium 提供 API 並支援多種語言 (HTML, Java、C#、Perl、PHP、Python、Ruby) 方便寫 test cases。
    • Selenium-Grid:平行測試、跨瀏覽器測試。

    Selenium 指令 (Selenese)
    指令有三種型態:(1) 動作 Actions;(2) 存取 Accessors;(3) 判斷 Assertions。
    • 動作:用來操作頁面,例如:點擊(click)、輸入(type) ...等動作。如果動作執行出問題,這個測試會被中止。
    • 存取:查看目前頁面的狀態,或者把頁面內容存起來,例如 storeTitle。也會被用來判斷(Assertions)。
    • 判斷:類似存取(Accessors),但更進一步地做些檢查,例如看目前頁面的標題是否為X,某個欄位是否被勾選...等。有三種用法:宣稱(assert)、驗證(verify)、等候(waitFor)。宣稱某個正確的內容時,若實際內容與宣稱內容不符合,此測試會中斷。驗證則稍微寬鬆一些,測試會繼續執行而把錯誤存在紀錄的 log 中。等候則用來確認某些內容出現在頁面上(常見的情況是搭配有 ajax 的頁面使用),當該內容未在設定的時間內出現時 (timeout),則中斷測試。
    (... 閱讀「Selenium Docs (1) - 基本介紹」全文)

    2009年4月28日 星期二

    Selenium Note

    [Tutorial] [Documentation] [API] [Forum]

    selenium 不能測的功能:
    (1) 檔案上傳 - 無法控制 windows file chooser
    (2) ActiveX 的操作
    (3) applet 的操作
    (4) Flash - [對策]
    (5) 切換 domain [The Same Origin Policy]

    Reference:
  • Google Developer Talk: http://tw.youtube.com/watch?v=EDb8yOM3Vpw

  • C. Titus Brown, Gheorghe Gheorghiu, Jason Huggins, An Introduction to Testing Web Applications with twill and Selenium , O'REILLY, 2007. [Google Preview]
  • (... 閱讀「Selenium Note」全文)

    2008年9月25日 星期四

    Redirect 網址的方法

    Reference: [WEB][PHP][SEO] 轉導、轉向(Redirect)網址的方法

    在將網站更換成新網址的情況下,可能會在舊網址上使用到一些『轉導網址』的方法,以便將原本的使用者及其流量引導到新的網址去。
    以下整理、討論到幾種轉導(Redirect)網址的技術方法,並且探討該方法對 SEO 的影響:

    1. 使用 HTTP 通訊協定 301 Moved Permanently 來完成轉導網址 (永久轉址)
    (建議使用,不會對 SEO 有不良影響)
    PHP 程式範例:
    <?php
       header("HTTP/1.1 301 Moved Permanently");
       eader("Location: http://www.new-url.com/");
    ?>
    <p>The document has moved <a href="http://www.new-url.com/">here</a>.</p>
    • 註1: 使用者的瀏覽器必須根據 HTTP header 的 Location 欄位值(稱做URI)來轉導網址。
    • 註2: 除非 Request Method 是 HEAD,不然伺服器端回覆的訊息內必須包含一短的新網址的連結(hyperlink)資訊。
    (... 閱讀「Redirect 網址的方法」全文)