知識社群登入
Interface
by 蘇德宙, 2011-02-05 21:32, 人氣(1462)
Object.prototype.implements = function(funcName) {
  return this && this[funcName] && this[funcName] instanceof Function;
}
 
function isShape(obj) {
  return obj.implements("getArea");  // 檢查是否 implement getArea() 的介面 
}
 
function addAreas(s1, s2) {
  if (isShape(s1) && isShape(s2))
    return s1.getArea() + s2.getArea();
}