KJ *_*ice 9 javascript google-chrome
这是非常奇怪的行为(这似乎只发生在Mac上的Chrome上),其中大部分代码似乎完全被跳过,而应该具有值的变量被设置为"未定义".
以下是Chrome开发人员工具的屏幕截图.请注意,817行从未被击中!然而,833被击中,我们正在看的是一个被击中的异常,我抬起调用堆栈来找到这个混乱.还要注意,变量"loc","lon"和"tc"都是未定义的,这是不可能的,因为它们已经在第822,823/824和827/831行上进行了评估.如果计算中出现错误,这些变量的值应该是我理解的NaN.
这是实际的代码:
function getCircle2(latin, lonin, radius) {
var locs = new Array();
var lat1 = latin * Math.PI / 180.0;
var lon1 = lonin * Math.PI / 180.0;
var d = radius / 3956;
var x;
for (x = 0; x <= 360; x++) {
var tc = (x / 90) * Math.PI / 2;
var lat = Math.asin(Math.sin(lat1) * Math.cos(d) + Math.cos(lat1) * Math.sin(d) * Math.cos(tc));
lat = 180.0 * lat / Math.PI;
var lon;
if (Math.cos(lat1) == 0) {
lon = lonin; // endpoint a pole
}
else {
lon = ((lon1 - Math.asin(Math.sin(tc) * Math.sin(d) / Math.cos(lat1)) + Math.PI) % (2 * Math.PI)) - Math.PI;
}
lon = 180.0 * lon / Math.PI;
var loc = new VELatLong(lat, lon);
locs.push(loc);
}
return locs;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以对这个魔法有所启发吗?为什么断点会被忽略,变量在Mac上的Chrome中只有不正确的值!
编辑:
看来我修复了这个bug.我所做的只是在它自己的函数中隔离破坏代码,调用函数一次,如果它抛出异常我再次调用它,它似乎100%的时间工作.我仍然非常好奇这个问题的根本原因.
//new function to isolate the exception
function getCirclePointOnRadius(deg, lat1, lon1, d, attempt) {
attempt = attempt || 1;
var maxAttempts = 2;
try {
var tc = (deg / 90) * Math.PI / 2;
var lat = Math.asin(Math.sin(lat1) * Math.cos(d) + Math.cos(lat1) * Math.sin(d) * Math.cos(tc));
lat = 180.0 * lat / Math.PI;
var lon;
if (Math.cos(lat1) == 0) {
lon = lonin; // endpoint a pole
}
else {
lon = ((lon1 - Math.asin(Math.sin(tc) * Math.sin(d) / Math.cos(lat1)) + Math.PI) % (2 * Math.PI)) - Math.PI;
}
lon = 180.0 * lon / Math.PI;
var loc = new VELatLong(lat, lon);
return loc;
}
catch (e) {
console.log2('Error when gathering circle point "' + e + '", trying again', deg, lat1, lon1);
if (attempt < maxAttempts) {
return getCirclePointOnRadius(deg, lat1, lon1, ++attempt);
}
else {
return 0;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将原来保存逻辑(in getCircle2
)的循环替换为:
for (x = 0; x <= 360; x++) {
locs.push(getCirclePointOnRadius(x, lat1, lon1, d));
}
Run Code Online (Sandbox Code Playgroud)
看来您可能是变量提升的受害者http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html
第 833 行可能被提升为函数作用域内的变量名称声明。重构变量声明可能会解决问题。
归档时间: |
|
查看次数: |
501 次 |
最近记录: |