1. 阿里云>
  2. 云栖社区>
  3. 正文

ajax入门指南(五)

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)
                将字符串中所有正则表达式匹配的部分替换成指定的字符串
                pattern:正则表达式
                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());



相关文章

立即打开APP,浏览更多精彩内容