你的位置:首页 > Java教程

[Java教程]js模拟接口源码



<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <body> <script>var reg={ string:/^[\D]+/ } interfaces.interfacee=[]; interfaces.is=true;function interfaces(name,method){ function interface(name,method){ if(!reg.string.test(name)|| typeof name!=="string"||arguments.length<2||method.constructor!==Array) { throw "接口所有对象属性值不能是undefined,name===string,method===Array" } for(var i=0,len=method.length;i<len;i++) { if(!reg.string.test(method[i]||typeof method[i]!=="string")){throw "接口对象方法名必须是string类型;"} } this.name=name; this.methdos=method; interfaces.interfacee.push(arguments.callee) } return new interface(name,method); } var inter=interfaces("w",["tr","b"]); var inter2=interfaces("w",["tr","b"]); function test(){this.tr=function(){};this.b=function(){}};var test1=new test(); function test3(){this.tr=function(){};this.b=function(){}};var test2=new test3(); interfaces.intercheck=function(object){ if(arguments.length<2||typeof object !=="object"){throw "必须指定接口对象属性参数"} for(var i=1,j=0;i<arguments.length;i++,j++) { var interobj=arguments[i]; if(interobj.constructor!== interfaces.interfacee[j]){throw "指定的接口对象参数属性并不是一个接口对象"} for(var k=0;k<interobj.methdos.length;k++){ var inmethod=interobj.methdos[k]; if(!object.length){ if( !object[inmethod]||typeof object[ inmethod]!=="function" ) { throw "实现的接口抽象方法的对象的方法类型必须是function,此对象并没有实现接口抽象方法"} } } for(var u=0;u<object.length;u++){ if( !object[u][inmethod]||typeof object[u][ inmethod]!=="function" ) { throw "实现的接口抽象方法的对象的方法类型必须是function,此对象并没有实现接口抽象方法" } } } } interfaces.intercheck(test2,inter,inter2); </script> </body> </html>

前端交流群 162791594;