Javascript-форум (https://javascript.ru/forum/)
-   Сайт Javascript.ru (https://javascript.ru/forum/site/)
-   -   портирование функции (https://javascript.ru/forum/site/64468-portirovanie-funkcii.html)

diprom 13.08.2016 21:03

портирование функции
 
здравствуйте. подскажите, как более понятно переписать функцию
indexElementMatchingFunction. Не очень понятно, как передать функцию Apoint.

function grep (a, b, c)
{

var d, e = [],
f = 0,
g = a.length;
c = false;
for (; f < g; f++) d = !!b(a[f], f), c !== d && e.push(a[f]);
return e
}



function filterFirstMatchingElement(array, fn)
{
var element = grep(array, fn)[0];
return element;
}

function indexElementMatchingFunction(array, fn) {

var element = filterFirstMatchingElement(array, fn);
return array.indexOf(element);
}

function pointsEqual(currentPoint, Apoint)
{
return false; // may be true
}

currentIndex = indexElementMatchingFunction(currentPolygon, function(Apoint)
{
return pointsEqual(currentPoint, Apoint);
});

diprom 13.08.2016 21:23

Описание функции pointsEqual

function pointsEqual(point1, point2) {
if (!point1)
return false;
if (!point2)
return false;
// return point1.x == point2.x && point1.y == point2.y;
return numbersEqual(pointsDistance(point1, point2), 0);
}

function numbersEqual(num1, num2) {
return Math.abs(num2 - num1) < 1e-14;
}

function pointsDistance(point1, point2) {
return Math.pow(Math.pow(point2.x - point1.x, 2) + Math.pow(point2.y - point1.y, 2), 0.5);
}

diprom 14.08.2016 07:04

Это замыкание. Как его обойти?

diprom 14.08.2016 08:43

все решил, можно тему закрывать


Часовой пояс GMT +3, время: 15:41.