js-intersections icon indicating copy to clipboard operation
js-intersections copied to clipboard

How to code for optimizations/minimizations

Open iklotzko opened this issue 11 years ago • 0 comments

Often people will use your library with other software, goog closures, etc..., where functions change name and can't be referred to by a literal string with square bracket notation.

Example:

function aNiceProperName() {};
aNiceProperName(); // function is called without problem
window['aNiceProperName'].apply(null, []); // this will result in error (there's no function with this name because during compilation aNiceProperName may translate to M

Here is how I have obviated that for a very small portion of the code for only 2 intersect shape combinations. There's no error checking, it's minimal, but it applies the same ideas in the original code but it is "compilation friendly now".

var IntersectionMethodMap = {
        'intersectEllipseRectangle': function(context, params) {return Intersection.intersectEllipseRectangle.apply(context, params);},
        'intersectLineRectangle': function(context, params) {return Intersection.intersectLineRectangle.apply(context, params);}
    };
    Intersection.intersectShapes = function (shape1, shape2) {
        var ip1 = shape1.getIntersectionParams();
        var ip2 = shape2.getIntersectionParams();
        var result;
        if (ip1 != null && ip2 != null) {
            if (ip1.name == "Path") {
                result = Intersection.intersectPathShape(shape1, shape2);
            } else if (ip2.name == "Path") {
                result = Intersection.intersectPathShape(shape2, shape1);
            } else {
                var method;
                var params;
                if (ip1.name < ip2.name) {
                    method = "intersect" + ip1.name + ip2.name;
                    params = ip1.params.concat(ip2.params);
                } else {
                    method = "intersect" + ip2.name + ip1.name;
                    params = ip2.params.concat(ip1.params);
                }
                result = IntersectionMethodMap[method](null, params);
            }
        } else {
            result = new Intersection("No Intersection");
        }
        return result;
    };
'''

iklotzko avatar Mar 05 '13 16:03 iklotzko