Arc不是构造函数'错误
'Arc is not a constructor' 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;
};
}
我绞尽脑汁想解出这道题。帮助是非常感激的,谢谢! This
var finder = new RouteFinder(lats, lons);
在Arc 变量得到它的值(函数)之前执行。因此,在RouteFinder调用时,Arc
变量是未定义的,不能用作构造函数。
更新:它实际上是关于finder.getPairs();
,你试着叫它new Arc();
你需要像这样设置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;
}
问题是Arc
尚未定义。将函数表达式赋值(var Arc = function ...
)移到脚本的顶部,或者将它们转换为函数定义语句,如下所示
function Arc() {
this.startNode = new Node();
this.endNode = new Node();
this.weight = 0;
}
语句是提升的,它们的顺序无关紧要,但是对var
的赋值是按照它们在文件中出现的顺序发生的(声明是提升的,但赋值不是)。
同样适用于Route
和Node
函数。
在MDN上阅读更多关于变量提升的信息
相关文章:
- Chrome Javascript日期构造函数错误
- Highstock,错误:未捕获的类型错误:w[(中间值)(中间值)(中间值)]不是构造函数
- 错误 [对象 BlobConstructor]' 不是 safari 中的构造函数
- 具有默认参数值的 ES6 类构造函数上的 NodeJS 错误
- 创建 JavaScript 构造函数时的语法错误
- 断言使用new执行构造函数时抛出错误
- 带有拒绝调用和抛出错误的Promise构造函数
- 类型错误:createjs.Sprite不是构造函数
- 为什么React在类构造函数中使用setState方法时抛出错误
- 是否可以在函数构造函数中识别哪个对象调用它,并在错误的对象调用时中止创建
- 轨道控制 '三.球形不是构造函数的错误
- 清理在构造函数中引发错误的对象
- breeze.config.MetadataHelper 不是 Durandal 和 Breeze 的构造函数错误
- Javascript“构造函数”属性中的错误 - IE 8
- 主干.js - n捕获异常:语法错误:函数构造函数:无法编译函数
- JavaScript 构造函数在定义属性时返回解析错误
- 类型错误:游戏.HexRenderer 不是 Linux Mint 上的构造函数
- 继承和 TypeScript 错误:X 不是构造函数类型
- 对象构造函数语法错误
- 当我尝试在 ace.js 中创建 Range 对象时,抛出“非法构造函数”错误