'Arc不是构造函数'错误

时间:2014-08-01 19:07:38

标签: javascript oop syntax-error

我收到一条错误消息,指出Arc不是构造函数。我检查了我的代码,无法理解原因。

/*
Exception: Arc is not a constructor
RouteFinder/this.getPairs@Scratchpad/8:72:6
@Scratchpad/8:5:3
*/

此错误的其他答案表明这是因为Arc之前已定义,但我无法找到它的任何地方。这是一大块代码,因此向下滚动以查找错误 - 它被评论为

var lats = [ 51.445371, 51.45526, 51.426765, 51.441304 ]
var lons = [ -0.077581, -0.113248, -0.13091, -0.060596 ]
    var finder = new RouteFinder(lats, lons);

    finder.getPairs();
    var nodeList =  ''
    for(var count = 1; count < lats.length; count++) {
        nodeList += count;
    }

    finder.permutation("", nodeList);
    finder.removeDuplicateRoutes();
    var shortestRoute = finder.getShortestRoute();
    var finalRouteOrder = [];
    shortestRoute.nodeList.forEach(function(node) {
        finalRouteOrder.push(+node);
    });

    alert(finalRouteOrder);


var Route = function() {
    this.totalWeight = 0;
    this.nodeList = [];
};

var Node = function() {
    this.lat = 0;
    this.lon = 0;
    this.number = 0;
};

var Arc = function() {
    this.startNode = new Node();
    this.endNode = new Node();
    this.weight = 0;
};

function reverseString(initialString) {
    var reversed = '';
    for(var count = initialString.length -1; count > -1; count--) {
        reversed += initialString.charAt(count);
    }
    return reversed;
}

function calcDistance(lat1, lng1, lat2, lng2) {
    var earthRadius = 6371;
    var dLat = (lat2 - lat1)/180*Math.PI;
    var dLng = (lng2 - lng1)/180*Math.PI;
    var a  = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(lat1/180*Math.PI) * Math.cos(lat2/180*Math.PI) * Math.sin(dLng/2) * Math.sin(dLng/2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    var dist = earthRadius * c;
    return dist;
}

function RouteFinder(lats, lons) {
    this.latArray = lats;
    this.lonArray = lons;
    this.orders = [];
    this.arcArray = [];
    this.routes = [];
    this.shortestRoute;
    this.numOfPoints = lats.length - 1;
    this.shortestRouteLength = -1;

    this.getPairs = function() {
        var timesLooped = 0;
        for(var count1 = 0; count1 < this.numOfPoints; count1++) {
            for(var count2 = 0; count2 < (this.numOfPoints - count1); count2++) {

                //I get an error here for new Arc()

                this.arcArray.push(new Arc());
                this.arcArray[timesLooped].startNode = {
                    number: count1,
                };
                this.arcArray[timesLooped].endNode = {
                    number: this.numOfPoints - count2,
                };
                this.arcArray[timesLooped].weight = calcDistance(this.latArray[count1], this.lonArray[count1], this.latArray[this.numOfPoints - count2], this.lonArray[this.numOfPoints - count2]);
                timesLooped++;
            }
        }
    };
    this.permutation = function(prefix, str) {
        var n = str.length;
        if(n === 0) this.orders.push('0' + prefix + '0');
        else {
            for(var i = 0; i <n; i++) {
                this.permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n));
            }
        }
    };
    this.removeDuplicateRoutes = function() {
        var numberOfPermutations = this.orders.length -1;
        var temp;
        var size;
        var toRemove = [];
        for(var count1 = 0; count1 < numberOfPermutations; count1++) {
            for(var count2 = 0; count2 < (numberOfPermutations - count1); count2++) {
                if(this.orders[count1] == reverseString(this.orders[numberOfPermutations - count2])) {
                    toRemove.push(count1);
                }
            }
        }
        size = toRemove.length;
        for(var count3 = 0; count3 < size; count3++) {
            temp = toRemove[size - 1- count3];
            var index = this.orders.indexOf(temp);
            if(index > -1) {
                temp.splice(index, 1);
            }
        }
    };
    this.getShortestRoute = function() {
        var routesMade = 0;
        for(var routeNumber = 0; routeNumber < (this.orders.length -1); routeNumber++) {
            this.routes.push(new Route());
            this.routes[routesMade].totalWeight = 0;
            for(var count1 = 0; count1 < this.orders[routeNumber].length; count1++) {
                this.routes[routesMade].nodeList.push(+this.orders[routeNumber].charAt(count1));
            }
            for(var count2 = 1; count2 < this.orders[routeNumber].length; count2++) {
                for(var count3 = 0; count3 < this.arcArray.length; count3++) {
                    if(this.routes[routesMade].nodeList[count2 - 1] === this.arcArray[count3].startNode.number) {
                        if(this.routes[routesMade].nodeList[count2] === this.arcArray[count3].endNode.number) {
                            this.routes[routesMade].totalWeight += this.arcArray[count3].weight;
                        }
                    } else if (this.routes[routesMade].nodeList[count2 - 1] === this.arcArray[count3].endNode.number) {
                        if(this.routes[routesMade].nodeList[count2] === this.arcArray[count3].startNode.number) {
                            this.routes[routesMade].totalWeight += this.arcArray[count3].weight;
                        }
                    }
                }
            }
            if(!this.shortestRoute) {
                this.shortestRoute = this.routes[routesMade];
            } else if(this.routes[routesMade].totalWeight < this.shortestRoute.totalWeight) {
                this.shortestRoute = this.routes[routesMade];
            }
            routesMade++;
        }
        return this.shortestRoute;
    };
}

我正在试图解决这个问题。非常感谢帮助,谢谢!

3 个答案:

答案 0 :(得分:2)

这个

var finder = new RouteFinder(lats, lons);

在Arc 变量获取其值(函数)之前执行。因此,在RouteFinder调用时,Arc变量未定义且不能用作构造函数。

更新:实际上您尝试调用finder.getPairs();

的地方约为new Arc();

答案 1 :(得分:1)

你需要像这样拥有Route,Node和Arc:

function Route() {
    this.totalWeight = 0;
    this.nodeList = [];
}

function Node() {
    this.lat = 0;
    this.lon = 0;
    this.number = 0;
}

function Arc() {
    this.startNode = new Node();
    this.endNode = new Node();
    this.weight = 0;
}

答案 2 :(得分:0)

问题是Arc尚未定义。将函数表达式赋值(var Arc = function ...)移动到脚本的顶部或将它们转换为像这样的函数定义语句

function Arc() {
    this.startNode = new Node();
    this.endNode = new Node();
    this.weight = 0;
}

语句已被悬挂且其顺序无关紧要,但var的分配按照它们出现在文件中的顺序发生(声明已悬挂,但分配不是)。

同样适合您的RouteNode功能。

MDN上阅读有关var hoisting的更多信息。