JavaScript 中的接口与封装:概念、应用与实现
1. 接口类的介绍
在 JavaScript 中,接口类是一个非常实用的工具,它可以帮助我们确保对象实现了特定的方法。以下是接口类的定义:
// Constructor.
var Interface = function(name, methods) {
if(arguments.length != 2) {
throw new Error("Interface constructor called with " + arguments.length +
"arguments, but expected exactly 2.");
}
this.name = name;
this.methods = [];
for(var i = 0, len = methods.length; i < len; i++) {
if(typeof methods[i] !== 'string') {
throw new Error("Interface constructor expects method names to be "
+ "passed in as a string.");
}
this.methods.push(methods[i]);
}
};
// Static class method.
Interface.ensureImplement
JavaScript接口与封装详解
超级会员免费看
订阅专栏 解锁全文
17万+

被折叠的 条评论
为什么被折叠?



