function Interface(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; i < methods.length; 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]); } } // 示例用法 var MyInterface = new Interface('MyInterface', ['method1', 'method2']);
MyInterface.prototype.method1 = function() { // 方法1的实现 }; MyInterface.prototype.method2 = function() { // 方法2的实现 };
本文为翻滚的胖子原创文章,转载无需和我联系,但请注明来自猿教程iskeys.com