七段

无论怎样,请让我先感谢一下国家。

BlogJava 首页 新随笔 联系 聚合 管理
  35 Posts :: 2 Stories :: 7 Comments :: 0 Trackbacks

#

他们有什么区别?我得意的笑 囧……
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 阅读(1227) | 评论 (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 阅读(239) | 评论 (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 阅读(610) | 评论 (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 阅读(595) | 评论 (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 阅读(310) | 评论 (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 人发表留言,猛击->>这里<<-参与讨论


JavaEye推荐




文章来源:http://sevenduan.javaeye.com/blog/507354
posted @ 2009-10-31 14:49 sevenduan 阅读(388) | 评论 (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 人发表留言,猛击->>这里<<-参与讨论


JavaEye推荐




文章来源:http://sevenduan.javaeye.com/blog/507054
posted @ 2009-10-31 14:49 sevenduan 阅读(96) | 评论 (0)编辑 收藏

请给Array本地对象增加一个原型方法,它的用途是删除数组条目中重复的条目(可能有多个),返回值是一个仅包含被删除的重复条目的新数组。
    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 人发表留言,猛击->>这里<<-参与讨论


JavaEye推荐




文章来源:http://sevenduan.javaeye.com/blog/506830
posted @ 2009-10-31 14:49 sevenduan 阅读(247) | 评论 (0)编辑 收藏

做为一个java coder,除了eclipse, firefox,也是Outlook的重度使用者。
熟用以下快捷键是request code review, reply code review的制胜法宝。

创建邮件。  Ctrl+Shift+M
创建便笺。  Ctrl+Shift+N
新建MO文档。  Ctrl+Shift+H
检查姓名。  Ctrl+K
面板切换。  F6
答复邮件。  Ctrl+R
移动项目。  Ctrl+Shift+V
reply all。 Ctrl+Shift+R
转发邮件。 Ctrl+F
“flag”。  Insert
发送。          Alt+S

拼写检查。  F7
查找或替换。  F4
增大缩进。  Ctrl+T
减小缩进。 Ctrl+Shift+T
下划线。 Ctrl+U
增大字号。 Ctrl+]
减小字号。 Ctrl+[或Ctrl+Shift+<
清除格式。  Ctrl+空格键
文本左对齐。  Ctrl+L
文本居中对齐。 Ctrl+E
文本右对齐。 Ctrl+R

已有 0 人发表留言,猛击->>这里<<-参与讨论


JavaEye推荐




文章来源:http://sevenduan.javaeye.com/blog/506109
posted @ 2009-10-31 14:49 sevenduan 阅读(281) | 评论 (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() 执行时,浏览器先创建整个scripting环境(就像一个新页面),导入scope chain中所有变量,执行script,gc, 最后导出所有变量到当前环境。(in a word, cost much)另外,js engine还不能对它们进行cache优化。

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 人发表留言,猛击->>这里<<-参与讨论


JavaEye推荐




文章来源:http://sevenduan.javaeye.com/blog/505272
posted @ 2009-10-31 14:49 sevenduan 阅读(163) | 评论 (0)编辑 收藏

仅列出标题
共4页: 上一页 1 2 3 4 下一页