幾種javascript的改進(jìn)寫(xiě)法

          1遍歷數(shù)組
          通常寫(xiě)法:(更改為“推薦寫(xiě)法”,隨筆修改于2007年11月26日
          var myArray=new Array();
          myArray.push(
          1);
          myArray.push(
          "1");
          for(var i=0;i<myArray.length;i++)
          alert(myArray[i]);
          推薦寫(xiě)法:(更改為“非推薦寫(xiě)法”,隨筆修改于2007年11月26日
          var myArray=new Array();
          myArray.push(
          1);
          myArray.push(
          "1");
          for(var i in myArray)
          alert(myArray[i]);
          原因:代碼量少(原因見(jiàn)ke的評(píng)論,隨筆修改于2007年11月26日

          2定義構(gòu)造函數(shù)
          通常寫(xiě)法:
          function Person(name,sex)
          {
              
          var action='run';
              
          this.name=name;
              
          this.sex=sex;
              
          this.run=function(){
              alert(action);
              };
          }
          推薦寫(xiě)法:
          function Person(name,sex)
          {
              
          this.name=name;
              
          this.sex=sex;
              
          this.action='run';
          }
          Person.prototype.run
          =function(){
              alert(
          this.action);
          };
          原因:避免重復(fù)創(chuàng)建函數(shù),避免使用閉包
          3判斷一個(gè)js對(duì)象是否支持某個(gè)屬性或方法
          錯(cuò)誤寫(xiě)法:
          var person=new Person('lzq','男');
          if(person.name)
          {
             alert('name屬性存在');
          }
          正確寫(xiě)法:
          var person=new Person('lzq','男');
          if(typeof(person.name)!='undefined')
          {
            alert('name屬性存在');
          }

          原因:當(dāng)person.name=null,0,false時(shí)測(cè)試失效
          4在IE中根據(jù)name屬性取得SPAN元素
          錯(cuò)誤寫(xiě)法:

          var domObjs=document.getElementsByName('nameStr');
          正確寫(xiě)法:
          function getElementsByNAME(name)
          {
             returns 
          = new Array();
             
          var e = document.getElementsByTagName('span');
             
          for(i = 0; i < e.length; i++) {
                                        
          if(e[i].getAttribute("name"== name) {
                                                    returns[returns.length] 
          = e[i];
                                        }
                          }
                          
          return returns;
          }
          var domObjs=getElementByNAME('nameStr'); 
          原因:w3c規(guī)范中g(shù)etElementsByName是按著name屬性進(jìn)行檢索的,而MS的IE卻是按著id來(lái)檢索,導(dǎo)致不能得到應(yīng)該得到的Elements。
          5得到字符串所占的字符個(gè)數(shù)
          錯(cuò)誤寫(xiě)法:
          var str="Java我選擇,我喜歡!";
          var charLength=str.length;
          正確寫(xiě)法:
          function   getLen(str) 
          {
                 
          var totallength=0;  
                 
          for (var i=0;i<str.length;i++)
                 
          {
                  
          var intCode=str.charCodeAt(i);    
                  
          if (intCode>=0&&intCode<=128
                  
          {
                       totallength
          =totallength+1//非中文單個(gè)字符長(zhǎng)度加 1
                  }

                  
          else 
                  
          {
                       totallength
          =totallength+2//中文字符長(zhǎng)度則加 2
                  }

                 }
           
                 
          return totallength;
           }

          var str="Java我選擇,我喜歡!";
          var charLength=getLen(str);
          原因:一個(gè)漢字占兩個(gè)字符

          posted on 2007-11-25 17:37 我為J狂 閱讀(2241) 評(píng)論(15)  編輯  收藏 所屬分類: JavaScript

          評(píng)論

          # re: 幾種javascript的改進(jìn)寫(xiě)法 2007-11-25 21:13 bean

          第一條、第三條嚴(yán)重質(zhì)疑。
          第一條:
          for(var i=0,j=myArray.length;i<j;i++)
          alert(myArray[i]);
          第三條:
          if(Person.name){}  回復(fù)  更多評(píng)論   

          # re: 幾種javascript的改進(jìn)寫(xiě)法 2007-11-25 21:20 我為J狂

          @bean
          置疑什么?最好說(shuō)得清楚一些,有點(diǎn)看不懂你的寫(xiě)法。  回復(fù)  更多評(píng)論   

          # re: 幾種javascript的改進(jìn)寫(xiě)法[未登錄](méi) 2007-11-25 21:31 ke

          Don't aggree on the third rule, closure can provide better encapsulation.
          The rule should be use closure to support private properties when possible.
          function Person(name,sex)
          {
          var action='run';//private property
          this.run=function(){//public method
          alert(action);
          };
          }  回復(fù)  更多評(píng)論   

          # re: 幾種javascript的改進(jìn)寫(xiě)法 2007-11-25 21:46 我為J狂

          @ke
          Thank you for your advice!Are you a foreigner?  回復(fù)  更多評(píng)論   

          # re: 幾種javascript的改進(jìn)寫(xiě)法[未登錄](méi) 2007-11-25 21:48 ke

          For the first rule, it is a tirck, ARRAY IS KIND OF OBJECT

          var arr = [];
          arr[-1]=-1;//add an property named "-1" on arr OBJECT
          arr[1]=1; //put a value whose index is 1 into arr ARRAY
          for(var k in arr)alert("arr[" + k + "]=" + arr[k]);
          alert("length = " + arr.length);
          for(var i=0;i<arr.length;i++)alert("arr[" + i + "]=" +arr[i]);  回復(fù)  更多評(píng)論   

          # re: 幾種javascript的改進(jìn)寫(xiě)法 2007-11-25 21:54 我為J狂

          @ke
          Do you agree with me or not?  回復(fù)  更多評(píng)論   

          # re: 幾種javascript的改進(jìn)寫(xiě)法[未登錄](méi) 2007-11-25 21:54 ke

          The fifth one, I tested your code in FF2 and IE7, both alert "12".

          var str="Java我選擇,我喜歡!";
          var charLength=str.length;

          May be there are problem in lower version.  回復(fù)  更多評(píng)論   

          # re: 幾種javascript的改進(jìn)寫(xiě)法[未登錄](méi) 2007-11-25 21:58 ke

          @我為J狂
          I don't quite aggree on the 1st rule, there are advice on this problem in mozilla.
          http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Statements:for...in
          "Although it may be tempting to use this as a way to iterate over an Array, this is a bad idea. The for...in statement iterates over user-defined properties in addition to the array elements, so if you modify the array's non-integer or non-positive properties (e.g. by adding a "foo" property to it or even by adding a method or property to Array.prototype), the for...in statement will return the name of your user-defined properties in addition to the numeric indexes. Also, because order of iteration is arbitrary, iterating over an array may not visit elements in numeric order. Thus it is better to use a traditional for loop with a numeric index when iterating over arrays."  回復(fù)  更多評(píng)論   

          # re: 幾種javascript的改進(jìn)寫(xiě)法 2007-11-25 22:05 我為J狂

          @ke
          呵呵,太感謝您了!受益匪淺。  回復(fù)  更多評(píng)論   

          # re: 幾種javascript的改進(jìn)寫(xiě)法 2007-11-26 09:16 我為J狂

          @ke
          我接受您的建議:針對(duì)第一條:遍歷數(shù)組的推薦方法應(yīng)該是for循環(huán),而不是for...in循環(huán)。  回復(fù)  更多評(píng)論   

          # re: 幾種javascript的改進(jìn)寫(xiě)法 2007-11-26 09:46 Tiger F

          我認(rèn)為第5條不正確,javascript中使用的應(yīng)該是unicode,情況與java類似。這樣的做法反而會(huì)得到錯(cuò)誤的結(jié)果。  回復(fù)  更多評(píng)論   

          # re: 幾種javascript的改進(jìn)寫(xiě)法 2007-11-26 09:51 Tiger F

          又看了一遍,可能是我理解錯(cuò)了。
          這個(gè)做法是想要確定GBK編碼情況下字符串需要的存儲(chǔ)空間大小。不過(guò)我還是覺(jué)得這樣不好,要是后臺(tái)系統(tǒng)要采用utf8或者utf16呢。增加了系統(tǒng)部署的限制。  回復(fù)  更多評(píng)論   

          # re: 幾種javascript的改進(jìn)寫(xiě)法 2007-11-26 12:22 我為J狂

          @Tiger F
          第五條是用于前臺(tái)頁(yè)面顯示,與后臺(tái)程序沒(méi)有關(guān)系。例如用來(lái)使某個(gè)層(DIV)的寬度隨著其中的字符長(zhǎng)度而變化。  回復(fù)  更多評(píng)論   

          # re: 幾種javascript的改進(jìn)寫(xiě)法 2007-11-28 20:44 金大為

          1.排開(kāi)性能問(wèn)題不說(shuō),你這樣做法時(shí)非常危險(xiǎn)的。很多人都有擴(kuò)充Array,String等原生對(duì)象的習(xí)慣。
          for in 可能將這些擴(kuò)充的成員函數(shù)也遍歷出來(lái)

          人外 var myArray=[];


          2.基本認(rèn)同
          3.基本認(rèn)同
          4.。。。
          5.。。。  回復(fù)  更多評(píng)論   

          # re: 幾種javascript的改進(jìn)寫(xiě)法 2007-12-26 18:15 臺(tái)灣

          支持一下,點(diǎn)了個(gè)AD  回復(fù)  更多評(píng)論   

          <2007年11月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(11)

          隨筆分類(48)

          文章分類(29)

          常去逛逛

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 棋牌| 兴安盟| 蓬莱市| 鄱阳县| 洛扎县| 六枝特区| 同仁县| 吉木萨尔县| 汉沽区| 开江县| 诸暨市| 博客| 长兴县| 金湖县| 定南县| 象山县| 莱西市| 东平县| 宝应县| 虎林市| 鸡西市| 江西省| 革吉县| 武胜县| 荆州市| 海盐县| 互助| 祥云县| 通辽市| 满洲里市| 康保县| 泾川县| 昌乐县| 莫力| 行唐县| 化州市| 东宁县| 孟州市| 民勤县| 双城市| 莱阳市|