另一替换接口是由提供bob.js:
1.检查接口实现:
var iFace = { say: function() { }, write: function() { } };
var obj1 = { say: function() { }, write: function() { }, read: function() { } };
var obj2 = { say: function() { }, read: function() { } };
console.log('1: ' + bob.obj.canExtractInterface(obj1, iFace));
console.log('2: ' + bob.obj.canExtractInterface(obj2, iFace));
// Output:
// 1: true
// 2: false
2.从对象中提取接口和仍能正常执行功能:
var obj = {
msgCount: 0,
say: function (msg) { console.log(++this.msgCount + ': ' + msg); },
sum: function (a, b) { console.log(a + b); }
};
var iFace = { say: function() { } };
obj = bob.obj.extractInterface(obj, iFace);
obj.say('Hello!');
obj.say('How is your day?');
obj.say('Good bye!');
// Output:
// 1: Hello!
// 2: How is your day?
// 3: Good bye!
我喜欢你的gravatar :) – alex 2009-09-14 04:06:31