七段

          無論怎樣,請讓我先感謝一下國家。

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            35 Posts :: 2 Stories :: 7 Comments :: 0 Trackbacks

          #

          他們有什么區(qū)別?我得意的笑 囧……
          1, null vs undefined
          2, new Object vs new Object()
          3, function foo(){} vs var foo=function foo(){}
          4,var a=b=undefined; vs var a,b;
          5,
          1 function Foo(){
          2 return true;
          3 }
          VS
          function Foo(){
          return 
                    
          true;
          }
          6, var a =[[1,2,3],[1,2,3],[1,2,3]]
          a[1][2] VS a[1,2]

          posted @ 2009-12-13 22:16 sevenduan 閱讀(1248) | 評論 (1)編輯 收藏

          refer to : http://mir.aculo.us/2009/11/08/6-easy-things-you-can-do-to-improve-your-javascript-runtime-performance/

          #1 avoid function calls
          #2 embrace the language: []>array, {} > object
          #3 loop: no loop > loop
          #4 cache globals: function(){var w =window;}
          #5 expression tuning: false move to before &&, true move to before ||
          #6 what not to use: e.g. with , eval, try catch,

          posted @ 2009-12-13 16:54 sevenduan 閱讀(262) | 評論 (0)編輯 收藏

          1    c();
          2             //a();//runtime error: a is not a function
          3             //b();//runtime error: b is not defined
          4             function c(){};//c will be defined and assigned value when pre-compile
          5             var a = function b(){ //b will be delete, a will be defined when pre-compile, a assigned value when runtime
          6             };
          7             a();
          posted @ 2009-12-11 16:44 sevenduan 閱讀(633) | 評論 (0)編輯 收藏

           1 function buildFunction(productList, productWeight){
           2                 var totalweight = eval(productWeight.join("+"))
           3                 var weighedProducts = new Array()
           4                 var currentProducts = 0
           5                 while (currentProducts < productList.length) {
           6                     for (i = 0; i < productWeight[currentProducts]; i++) {
           7                         weighedProducts.push(productList[currentProducts]);
           8                     }
           9                     currentProducts++
          10                 }
          11                 return function(){
          12                     var randomnumber = Math.floor(Math.random() * totalweight)
          13                     return (weighedProducts[randomnumber]);
          14                 };
          15             };
          16             var productList = ["AK-47""Blade""Food""ByondGod"]
          17             var productWeight = [2002041];
          18             var myFun = buildFunction(productList, productWeight);
          19             for (var i = 0; i < 100; i++
          20                 document.writeln((i+1)+":"+myFun())
          posted @ 2009-12-11 13:56 sevenduan 閱讀(633) | 評論 (0)編輯 收藏

            (function(){
                          
          var uuid = 0;
                          
          var NEW = 0, PENDING = 1, FINISH = 2;
                          
          var RemoteRule = window.RemoteRule = function(fn, options){
                              
          this.id = uuid++;
                              
          this.fn = fn;
                              
          this.para = options.requestPara;
                              
          this.showTips = function(){
                                  options.showTips();
                              }
                          }
                          
                          
          var RemoteValidator = window.RemoteValidator = function(){
                              
          this.rules = {};
                              
          this.status = {};
                          }
                          RemoteValidator.prototype 
          = {
                              addRule: 
          function(rule){
                                  
          this.rules[rule.id] = rule;
                                  
          this.status[rule.id] = NEW;
                              },
                              reset: 
          function(){
                                  
          this.rules = {};
                                  
          this.status = {};
                              },
                              validate: 
          function(callBack){
                                  
          var self = this;
                                  
          for (var id in self.rules) {
                                      
          var rule = self.rules[id];
                                      
          var updateFn = (function(){
                                          
          return function(data){
                                              
          if (data) {
                                                  
          delete self.status[rule.id];
                                              }
                                              
          else {
                                                  self.hasError 
          = true;
                                              }
                                              
          if (self.hasError) {
                                                  rule.showTips();
                                              }
                                              
          var isEmpty = true;
                                              
          for (var id in self.status) {
                                                  isEmpty 
          = false;
                                                  
          break;
                                              }
                                              
          if (isEmpty) {
                                                  callBack();
                                              }
                                          }
                                      })();
                                      self.status[rule.id] 
          = PENDING;
                                      rule.fn(rule.para, updateFn);
                                      
                                  }
                              }
                          }
                          
                      })();
                      
                      
          var dwrFnMock = function(para, callBack){
                          setTimeout(
          function(){
                              
          if (para.value > 0
                                  callBack(
          true);
                              
          else 
                                  callBack(
          false);
                          }, 
          1000);
                      };
                      
          var validator1 = new RemoteValidator();
                      validator1.addRule(
          new RemoteRule(dwrFnMock, {
                          requestPara: {
                              value: 
          1
                          },
                          showTips: 
          function(){
                              alert(
          "hasError!");
                          }
                      }));
                      validator1.validate(
          function(){
                          alert(
          "submit");
                      });
                      
          var validator2 = new RemoteValidator();
                      validator2.addRule(
          new RemoteRule(dwrFnMock, {
                          requestPara: {
                              value: 
          -1
                          },
                          showTips: 
          function(){
                              alert(
          "hasError!");
                          }
                      }));
                      validator2.validate(
          function(){
                          alert(
          "submit");
                      })
          posted @ 2009-12-08 11:28 sevenduan 閱讀(328) | 評論 (0)編輯 收藏

          jQuery Common Coding tips:
          1, less code by chain coding
          2, Use data method instead of storing data inside the DOM.
              
           $('selector').data('meaningfullname', 'this is the data I am storing');
          // then later getting the data with
          $('selector').data('meaningfullname');

          3, If you are Manipulating the DOM a lot, use livequery.(1.3)
           
              $('div.edit').livequery('click', function(){
          //go into edit mode
          });

          4, Use classes as flags:With jQuery you can add a class with the addClass method and then check later if an element has the class with the hasClass method.
          5, use same function name to handle different arguments
          6, pass options for configuration data
          7, test your code by screw.unit
          8, make most jQuery code into resuable plugins

          jQuery plugin pattern tips:
          (from: http://www.learningjquery.com/2007/10/a-plugin-development-pattern)
             1.  Claim only a single name in the jQuery namespace
             2. Accept an options argument to control plugin behavior
             3. Provide public access to default plugin settings
             4. Provide public access to secondary functions (as applicable)
             5. Keep private functions private
             6. Support the Metadata Plugin

          已有 0 人發(fā)表留言,猛擊->>這里<<-參與討論


          JavaEye推薦




          文章來源:http://sevenduan.javaeye.com/blog/507354
          posted @ 2009-10-31 14:49 sevenduan 閱讀(409) | 評論 (0)編輯 收藏

          Should we do RegEx or not?
          *pros:
          save time and less efforts
          less code
          *cons:
          sometimes heavyweight or involves heavy processing
          complicated RegEx hidden bugs, hard to read/write

          In a word, we need to balance the pros and cons above before make a descision.
          How to parse RegEx?
          //TODO:


          已有 0 人發(fā)表留言,猛擊->>這里<<-參與討論


          JavaEye推薦




          文章來源:http://sevenduan.javaeye.com/blog/507054
          posted @ 2009-10-31 14:49 sevenduan 閱讀(105) | 評論 (0)編輯 收藏

          請給Array本地對象增加一個原型方法,它的用途是刪除數(shù)組條目中重復的條目(可能有多個),返回值是一個僅包含被刪除的重復條目的新數(shù)組。
              var hashCode = function(element){
          return element.sort().toSource();
          }
          Array.prototype.dell = function(hashCode){
          var deleList = [];
          var obj = {};
          do {
          var ele = this.pop();
          var key = hashCode(ele);
          if (obj[key]) {
          deleList.push(ele);
          }
          else {
          obj[key] = ele;
          }
          }
          while (this.length > 0);
          for (var key in obj) {
          this.push(obj[key]);
          }
          return deleList;
          }
          var list = [[3, 1], [1, 2], [1, 3]]
          expect([[1, 3]]).to(equal, list.dell(hashCode));
          expect([[1, 2], [1, 3]].sort()).to(equal, list.sort());


          已有 0 人發(fā)表留言,猛擊->>這里<<-參與討論


          JavaEye推薦




          文章來源:http://sevenduan.javaeye.com/blog/506830
          posted @ 2009-10-31 14:49 sevenduan 閱讀(269) | 評論 (0)編輯 收藏

          做為一個java coder,除了eclipse, firefox,也是Outlook的重度使用者。
          熟用以下快捷鍵是request code review, reply code review的制勝法寶。

          創(chuàng)建郵件。  Ctrl+Shift+M
          創(chuàng)建便箋。  Ctrl+Shift+N
          新建MO文檔。  Ctrl+Shift+H
          檢查姓名。  Ctrl+K
          面板切換。  F6
          答復郵件。  Ctrl+R
          移動項目。  Ctrl+Shift+V
          reply all。 Ctrl+Shift+R
          轉發(fā)郵件。 Ctrl+F
          “flag”。  Insert
          發(fā)送。          Alt+S

          拼寫檢查。  F7
          查找或替換。  F4
          增大縮進。  Ctrl+T
          減小縮進。 Ctrl+Shift+T
          下劃線。 Ctrl+U
          增大字號。 Ctrl+]
          減小字號。 Ctrl+[或Ctrl+Shift+<
          清除格式。  Ctrl+空格鍵
          文本左對齊。  Ctrl+L
          文本居中對齊。 Ctrl+E
          文本右對齊。 Ctrl+R

          已有 0 人發(fā)表留言,猛擊->>這里<<-參與討論


          JavaEye推薦




          文章來源:http://sevenduan.javaeye.com/blog/506109
          posted @ 2009-10-31 14:49 sevenduan 閱讀(294) | 評論 (0)編輯 收藏

          Efficient JavaScript coding
          1, 盡可能選擇高效的method
          e.g.
          如果沒有必要,可以不用regular expression
          String.indexOf, String.lastIndexOf > String.match, String.search, String.replace

          2, 面對large loop就要斤斤計較
          2.1 Create once, use repeatedly
          for( var i = 0, oNode; oNode = oElement.childNodes[i]; i++ ) {
          if( oNode.nodeValue.match(/^\s*extra.*free/g) ) {
          //this creates the expression
          //it will be cached and re-used next time through the loop
          }
          }
          for( var i = 0, oNode; oNode = oElement.childNodes[i]; i++ ) {
          if( oNode.nodeValue.match(new RegExp(“^\s*extra.*free”,”g”)) ) {
          //this will always create a new copy of the expression,
          //and is generally much slower than creating static expressions.
          }
          }

          2.2 Referencing element once, use repeatedly
          var _table =$("#tableId")
          for (var index in json) {
          otherFun(_table, json[index]);
          };


          3 eval is evil
          Eval 或者 new Function() 執(zhí)行時,瀏覽器先創(chuàng)建整個scripting環(huán)境(就像一個新頁面),導入scope chain中所有變量,執(zhí)行script,gc, 最后導出所有變量到當前環(huán)境。(in a word, cost much)另外,js engine還不能對它們進行cache優(yōu)化。

          4 less is more
          Less code, short naming
          Only add event listener what you need

          5 do less
          Take a short circuit
          e.g
          var logger=window.console && window.console.dir
          var logger=window.console || {}

          less XHR calling
          e.g. enable cache for the same request

          6 Reduce reflow
          每當添加element到document里,browser就會reflow整個頁面去計算如何重新定位和渲染。

          7,cache
          Enable cache for duplicated XHR calling
          Enable cache for js script file, so move out jscript from jsp to js file.

          Reference:
          http://slowjavascript.com/JavaScript_Performance_Rocks_Checklist.pdf


          已有 0 人發(fā)表留言,猛擊->>這里<<-參與討論


          JavaEye推薦




          文章來源:http://sevenduan.javaeye.com/blog/505272
          posted @ 2009-10-31 14:49 sevenduan 閱讀(184) | 評論 (0)編輯 收藏

          僅列出標題
          共4頁: 上一頁 1 2 3 4 下一頁 
          主站蜘蛛池模板: 丰台区| 怀宁县| 阿拉善盟| 林甸县| 沐川县| 杭锦旗| 清水县| 武宣县| 平原县| 巴塘县| 印江| 通化市| 安乡县| 天门市| 麻江县| 九龙县| 昌吉市| 凯里市| 黔西县| 巴南区| 那坡县| 玉山县| 赤峰市| 宝鸡市| 涪陵区| 厦门市| 普洱| 仁怀市| 长沙县| 沈阳市| 旺苍县| 德惠市| 灌阳县| 绥化市| 泸州市| 墨竹工卡县| 五常市| 信阳市| 梅州市| 类乌齐县| 宁城县|