Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
function getRectangleArea(/**Object*/o)/**Number*/{
function getRectangleArea(/**Number*/width, /**Number*/height)/**Number*/{
return width * height;
}
if ('width' in o && 'height' in o) {
return getRectangleArea(o.width, o.height);
}
if ('x1' in o && 'x2' in o && 'y1' in o && 'y2' in o) {
return getRectangleArea(o.x2 - o.x1, o.y2 - o.y1);
}
}
console.log(getRectangleArea({width: 10, height: 10}));
console.log(getRectangleArea({x1: 10, x2: 20, y1: 10, y2: 20}));
* This source code was highlighted with Source Code Highlighter.
var getRectangleArea3 = polymorph();
getRectangleArea3.update(
function(width, height) {
return width * height;
},
function(x1, y1, x2, y2) {
return (x2-x1)*(y2-y1);
}
);
* This source code was highlighted with Source Code Highlighter.
Перегрузка функций в JS