ajax入門指南(五)

來源:互聯網
上載者:User
ajax學習入門指南,對ajax初學者應該有所協助。

Prototype基礎類:
       1. Class.create()
            樣本:
                var myClass = Class.create();

      2.  Object.extend(destination,source)
            樣本:
                var myClass = Class.create();
                myClass.prototype = {
                initialize: function() {
                 },
                 f1: function() {
                      alert("do f1()");
                 },
                 f2: function() {
                      alert("do f2()");
                 },
                 toString: function() {
                      return "myClass";
                 }
            };
            var mySubClass = Class.create();
            Object.extend(mySubClass.prototype, myClass.prototype);

       3. Object.inspect(object)
            返回目標對象的文字說明,如果對象沒有定義inspect方法,則預設返回object.toString()的值
            樣本:
            var myClass = Class.create();
            myClass.prototype = {
                 initialize: function() {
                 },
                 toString: function() {
                  return "myClass";
                 }
            };
            var obj = new myClass();
            alert(Object.inspect(obj));
        4.Function.prototype.bind(object)
            返回一個Function的執行個體,其結構與當前的Function對象完全相同,只是範圍已經轉移到參數指定的object對象上
            樣本:
            var myClass = Class.create();
            myClass.prototype = {
                 initialize: function() {
                 },
                 name: "myClass",
                 f1: function() {
                     alert(this.name + " from f1");
       }
            };
            var myClass2 = Class.create();
            myClass2.prototype = {
                 initialize: function() {
                 },
                 name: "myClass2",
                 f2: function() {
                      alert(this.name + " from f2");
                 }
            };
            var obj = new myClass();
            var obj2 = new myClass2();

            obj2.f2 = obj.f1.bind(obj2);
            obj2.f2(); // 輸出"myClass2 from f1"


        5.Function.prototype.bindAsEventListener
            與bind方法的功能相同,只不過bingAsEvevntListener用於綁定事件.
            樣本:
            var Watcher = Class.create();
            Watcher.prototype = {
                     initialize: function(buttonid, message) {
                              this.button = $(buttonid);
                              this.message = message;
                              // 將button的onclick和this對象的showMessage方法綁定起來
                              this.button.onclick =
                               this.showMessage.bindAsEventListener(this);
                     },
                     showMessage: function() {
                          alert(this.message);
                     }
                };
            var watcher = new Watcher('btn', 'clicked');

        6.PeriodicalExecuter類
            建立PeriodicalExecuter類的執行個體將會周期性地調用指定的方法
            function setTime() {
                 $('divTime').innerHTML = (new Date()).toLocaleString();
            }
            new PeriodicalExecuter(setTime, 1);
            第一個參數:調用的方法;第二個參數:間隔多少秒
字串處理(String對象擴充)
           1.String .prototype.gsub(pattern,replacement)
                將字串中所有Regex匹配的部分替換成指定的字串
                pattern:Regex
                replacement:用作替換的字串
                樣本:
                var str = "this is a test test";
                //輸出"this is a new new"
                alert(str.gsub(/test/,"new"));
               
            2.String.prototype.truncate(length,truncation)
                將字串截斷
                length:截斷後字串的長度,預設值為30
                trancation:截斷字串時,替代尾部的字串,預設是"..."
                樣本:
                 var str = "this is a test test";
                //輸出"this is ..."
                  alert(str.truncate(10));
                //輸出"this is a t..."
                alert(str.truncate(14));
                //輸出"this is***"
                alert(str.truncate(10,"***"));


            3.String.prototype.strip()
                刪除字串前後的空白字元
                樣本:
                var str="this is a test test  ";
                alert(str.strip().length);//19
                alert(str.length);//21

            4.String.prototype.stripTags()
                移除字串中所有的HTML和XML標籤
                樣本:
                var str = "<table><tr><td>stripTagsDemo</td></tr></table>";
               alert(str.stripTags());//輸出"stripTagsDemo"

            5.String.prototype.stripScripts()
                移除字串所有的<script></script>指令碼標記內容
                樣本:
                var str = "this is a test<script>alert('ok')<"+"/script>";
               alert(str.stripScripts());//輸出"this is a test"
6.String.prototype.evalScripts()
                執行在字串中找到的所有指令碼
                var str = "this is a test<script>alert('ok')<"+"/script>";
               str.evalScripts();//輸出"ok"

            7.String.prototype.escapeHTML()
                將字串中的所有HTML標記進行轉義
                樣本:
                var str = "<table><tr><td>stripTagsDemo</td></tr></table>";
               alert(str.escapeHTML());

            8.String.prototype.unescapeHTML()
                執行與escapeHTML()相反的操作


            9.String.prototype.toQueryParams()
                將查詢字串轉化為一個聯合數組
                樣本:
                var str = "a=1&b=2&c=3"
                var arr = str.toQueryParams();
                for(i in arr){
                     alert(arr[i]);//輸出1,2,3
               }

            10.String.prototype.toArray()
                把字串轉換成字元數組
                樣本:
                var str = "test"; 
                //依次輸出"t,e,s,t"
               str.toArray().each(
                    function(item){
                       alert(item);
                   }
               );

            11.String.prototype.extractScripts()
                從字串中提取出所有的<script>指令碼,以字串數組的形式返回
                樣本:
                var str = "this is a test<script>alert('ok')<"+"/script>";
               str += str;
                //輸出兩遍alert('ok')
               str.extractScripts().each(
                   function(item){
                       alert(item); 
      }
               );
 

            12.String.prototype.camelize()
                將一個以連字號串連的字串轉換成一個遵循駱駝命名法的字串
                樣本:
                var str = "this-is-a-test";  
                //輸出"thisIsATest"
               alert(str.camelize());



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.