惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
博客园_首页
WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
有赞技术团队
有赞技术团队
阮一峰的网络日志
阮一峰的网络日志
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
酷 壳 – CoolShell
酷 壳 – CoolShell
Y
Y Combinator Blog
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
Engineering at Meta
Engineering at Meta
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
N
Netflix TechBlog - Medium
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
G
Google Developers Blog
Last Week in AI
Last Week in AI

博客园 - Young.Jiang

Iframe的高级操作 设计模式学习:适配器模式 Linq学习笔记(2.3)——DLinq高级操作 Linq学习笔记(2.2)——深入DLinq查询 Linq学习笔记(2.1)——初识 DLinq Linq学习笔记(1.8)——Count、Sum、Min、Max、Average Linq学习笔记(1.7)——First、ElementAt、Any、all Linq学习笔记(1.6)——ToArray、ToList、ToDictionary、OfType Linq学习笔记(1.5)——group、Distinct、Union、Concat、Intersect、Except Linq学习笔记(1.4)——orderby、Reverse() Linq学习笔记(1.3)——Take、Skip Linq学习笔记(1.2)——select Linq学习笔记(1.1)——where 理解JavaScript中的对象 Asp.net Ajax 客户端编程(二)——Type 类,面向对象编程的开始 Asp.net Ajax 客户端编程(一)——对JS基本类型的扩展 提高.NET性能的最佳实践 EnterpriseLibrary 3.0的缓存应用程序块 EnterpriseLibrary 3.0的验证应用程序块
Javascript模拟c#方法重载
Young.Jiang · 2008-12-09 · via 博客园 - Young.Jiang

每次看John Resig的blog,都会有很大的收获!这次和大家一起分享一个Javascript模拟c#方法重载的例子。

 function loadMethod(object, name, fn){ 
            
var old = object[ name ]; 
            
if ( old ) 
                object[ name ] 
= function(){ 
                    
if ( fn.length == arguments.length ) 
                        
return fn.apply( this, arguments ); 
                    
else if ( typeof old == 'function' ) 
                        
return old.apply( this, arguments ); 
                }; 
            
else 
                object[ name ] 
= fn; 
        }

下面写一个在用户集合中查找特定用户数量的例子

function UserCount(){ 
            loadMethod(
this"find"function(list){ 
                
//查找所有数量
                var i=0;
                
return list.length;
            }); 
            loadMethod(
this"find"function(list,pwd){ 
                
//查找特定密码的用户数量
                var k=0;
                
for(var i=0;i<list.length;i++)
                {
                    
if(list[i].pwd==pwd)
                        k
++;
                }
                
return k;
            }); 
            loadMethod(
this"find"function(list,id, pwd){
                
//查找特定密码和id的用户数量 
                var k=0;
                
for(var i=0;i<list.length;i++)
                {
                    
if(list[i].id==id && list[i].pwd==pwd)
                        k
++;
                }
                
return k;
            })
        }

//定义集合
        var userlist=new Array();
        userlist.push({id:
"holygrace",pwd:"12345"});
        userlist.push({id:
"JiangYuYang",pwd:"2233"});
        userlist.push({id:
"liyanhong",pwd:"12345"});
        userlist.push({id:
"mayun",pwd:"adf"});
        userlist.push({id:
"wangzidong",pwd:"3456"});
        
        
var count=new UserCount()
        alert(
"find(userlist)找到\n"+count.find(userlist)+"");
        alert(
"find(userlist,\"12345\")找到\n"+count.find(userlist,"12345")+"");
        alert(
"find(userlist,\"holygrace\",\"12345\")找到\n"+count.find(userlist,"holygrace","12345")+"");