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

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Webroot Blog
Webroot Blog
U
Unit 42
A
About on SuperTechFans
宝玉的分享
宝玉的分享
月光博客
月光博客
C
CERT Recently Published Vulnerability Notes
P
Privacy International News Feed
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
P
Privacy & Cybersecurity Law Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Securelist
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Spread Privacy
Spread Privacy
L
Lohrmann on Cybersecurity
Apple Machine Learning Research
Apple Machine Learning Research
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
B
Blog
I
Intezer
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
V
V2EX
L
LangChain Blog
AI
AI
G
GRAHAM CLULEY
T
Tor Project blog
人人都是产品经理
人人都是产品经理
D
Docker
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
I
InfoQ
Y
Y Combinator Blog
C
Comments on: Blog
GbyAI
GbyAI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
H
Help Net Security
Vercel News
Vercel News
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿

博客园 - 温温恭人

[导入]:创建单色画笔 (Visual C#) [导入]在运行时创建位图 (Visual C#) [导入]创建笔 (Visual C#) [导入]。net图形操作(MSDN) [导入]如何动态生成table(javascript) [导入]javascript分页 [导入]贡献一个新浪的幻灯片(javascript) [导入]解析XML并生成表格 [导入]css控制div居中 [导入]精典的一句 [导入]通过简易的前台代码实现无限二级域名转向(来自无忧 biyuan老矣) [导入]巧妙的让web来执行“计划任务”(转 biyuan老矣 ) [导入]由浅到深了解JavaScript类 [导入]DHTML中重要的属性方法 - 温温恭人 [导入](转贴)正则表达式学习心得体会(5) [导入](转贴)正则表达式学习心得体会(4) [导入]正则表达式学习心得体会(3)(转) [导入]正则表达式学习心得体会(2) [导入](转贴)正则表达式学习心得体会(1) - 温温恭人 [导入]使用透明叠加法美化文件上传界面 (http://www.script8.com/bbs/thread.asp?tid=6) [导入]javascript总结 [导入]将程序集链接到 Word 或 Excel 文件 [导入]input高级限制级用法 [导入] js实现的动态多文件上传 (来自http://cms.bmw.net.cn) [导入]js用FileSystemObject 对象实现文件控制
[导入]用Javascript实现interface的类似功能
温温恭人 · 2007-06-19 · via 博客园 - 温温恭人

由于javascript是弱类型的语言,

参数不能限定类型,

所以提供一个方法isInstence(obj)来判断此对象是否是实现这个接口的类型.

注意,在实现这个接口之后并不会把这个接口加入到Function 的原形链中.

(此功能支持同时实现多个接口)
<script language="javascript">
Function.prototype.exChain=new Array();
Function.prototype.implements=function(itf)
{
 
     var innerObj=new this();
     for(i in itf.abstruct)
     {
      if(itf.abstruct[i].constructor==Function)
      {
       if(!innerObj[i])
       {
        throw new Error(9001,"请实现接口中的所有抽象函数。")
       }
      }
     }
     this.exChain.push(itf.constructor);
     this.exChain.push(this);
     innerObj=null;
     return this;
}
var Abstruct=function(){}
Function.prototype.isInstence=function(obj)
{
     for (var i=0;i<obj.constructor.exChain.length;i++)
     {
      if(this==obj.constructor.exChain[i])
      {
       return true;
      }
     }
     if(this==obj.constructor)
     {
      return true;
     }
     return false;
}

/////////////////////////////////////////////////////
InterfaceA=function()
{
     this.abstruct=new Abstruct();
     this.abstruct.show=function(){}
     //this.abstruct.getMsg=function(){}
}

InterfaceB=function()
{
     this.abstruct=new Abstruct();
     this.abstruct.showB=new Function();
}

var ClassA=function()
{
     this.show=function()
     {
      alert("ClassA");
     }
     this.showB=function()
     {
      alert("ClassA.showB")
     }
 
}.implements(new InterfaceA()).implements(new InterfaceB())

var o1=new ClassA();
o1.show();
o1.showB();
alert(InterfaceA.isInstence(o1));
alert(InterfaceB.isInstence(o1));
alert(ClassA.isInstence(o1));

var ClassB = function()
{
 
}
alert(ClassB.isInstence(o1));

</script>

文章来源:http://www.cnblogs.com/suiqirui19872005/archive/2007/05/22/756108.html