Javascript ';Arc不是构造函数';错误
我得到一个错误,说Arc不是构造函数。我检查了我的代码,不明白为什么Javascript ';Arc不是构造函数';错误,javascript,oop,syntax-error,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.42676
/*
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;
};
}
var lats=[51.445371,51.45526,51.426765,51.441304]
变量lons=[-0.077581,-0.113248,-0.13091,-0.060596]
var finder=新路由查找器(lats、lons);
finder.getPairs();
变量nodeList=''
对于(变量计数=1;计数-1;count--){
反向+=initialString.charAt(计数);
}
反向返回;
}
功能校准距离(lat1、lng1、lat2、lng2){
var earthRadius=6371;
var dLat=(lat2-lat1)/180*Math.PI;
变量dLng=(lng2-lng1)/180*Math.PI;
变量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=接地半径*c;
返回距离;
}
功能路由查找器(lats、lons){
this.latArray=lats;
this.lonArray=lons;
这个。订单=[];
this.arcArray=[];
this.routes=[];
这是最短的路线;
this.numOfPoints=lats.length-1;
this.shortestRouteLength=-1;
this.getPairs=函数(){
var timesLooped=0;
对于(var count1=0;count1
我拼命想解决这个问题。非常感谢您的帮助,谢谢 您需要有如下布线、节点和圆弧:
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;
}
您需要具有如下所示的管线、节点和圆弧:
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;
}
您需要具有如下所示的管线、节点和圆弧:
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;
}
您需要具有如下所示的管线、节点和圆弧:
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;
}
问题在于,
Arc
尚未定义。将函数表达式赋值(var Arc=function…
)移动到脚本顶部,或者将其转换为如下函数定义语句
function Arc() {
this.startNode = new Node();
this.endNode = new Node();
this.weight = 0;
}
语句被挂起,它们的顺序无关紧要,但对var
的赋值是按照它们在文件中出现的顺序进行的(声明被挂起,但赋值不被挂起)
您的路由
和节点
功能也是如此
阅读有关var吊装的更多信息。问题在于,
Arc
尚未定义。将函数表达式赋值(var Arc=function…
)移动到脚本顶部,或将其转换为函数定义语句