您的位置:首页 > 其它

模拟接口实现

2008-07-24 18:31 141 查看
/**
* @description 接口验证
* @param {Object}
* imp //实现类
* @param {Object}
* _interface 实现的接口 格式:['xx','yy']
* @example: var in_catDAO = ['eat', 'sleep'];
*
* function im_CatDAO(){

this.setName = function(){
alert('this is set name');
}

this.getName = function(){
alert('this is get name');
}

$Implement(this,in_CatDAO);//接口验证

return this;
}
}
*/

/**
* @description 接口验证
* @param {Object} imp //实现类
* @param {Object} _interface 实现的接口
*/
function $Implement(imp, _interface){
for (need in _interface) {
var methodExists = false; // 单一方法
for (method in imp) {
if (_interface[need] == method) {
methodExists = true;
break;
}
}
if (methodExists == false) {
alert("method:" + _interface[need] + "() doesn't exist!");
throw new Error("method:" + _interface[need] + "() doesn't exist!");// 抛出异常
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: