Javascript Can';t仅使用括号中的变量直接输入将dms转换为dd
我知道这可能被认为是一个重复的问题,但公平地说,上周我一直在研究这个问题,尝试每个人的方法,但都以同样的方式结束 基本上,它们都工作,然后直接输入值,但当您尝试引用变量时,它会给出一个“NAN”响应 例如,如果我想调用函数“LocationFormatter.DMSToDecimal(myvar)”,它将返回“NaN” 更新了答案,下面是代码片段。Javascript Can';t仅使用括号中的变量直接输入将dms转换为dd,javascript,jquery,string,function,variables,Javascript,Jquery,String,Function,Variables,我知道这可能被认为是一个重复的问题,但公平地说,上周我一直在研究这个问题,尝试每个人的方法,但都以同样的方式结束 基本上,它们都工作,然后直接输入值,但当您尝试引用变量时,它会给出一个“NAN”响应 例如,如果我想调用函数“LocationFormatter.DMSToDecimal(myvar)”,它将返回“NaN” 更新了答案,下面是代码片段。 //将'LongCombined'设置为数组, //保留单个项目的“类型”;e、 g`数字“,”字符串` var LongCombined=[81
//将'LongCombined'设置为数组,
//保留单个项目的“类型”;e、 g`数字“,”字符串`
var LongCombined=[81,36.96,0,'W'];
//将`LocationFormatter`设置为`Object`
var LocationFormatter={};
LocationFormatter.NORTH='N';
LocationFormatter.SOUTH='S';
LocationFormatter.EAST='E';
LocationFormatter.WEST='W';
LocationFormatter.roundToDecimal=函数(inputNum,numPoints){
var乘数=数学功率(10,numPoints);
返回Math.round(inputNum*乘数)/乘数;
};
LocationFormatter.DMSToDecimal=函数(度、分、秒、半球){
var ddVal=度+分/60+秒/3600;
ddVal=(半球==LocationFormatter.SOUTH | |半球==LocationFormatter.WEST)?ddVal*-1:ddVal;
返回LocationFormatter.roundToDecimal(ddVal,5);
};
document.getElementById(“demo1”).innerHTML=LocationFormatter.DMSToDecimal(81,36.96,0,'W');
//调用`LocationFormatter.DMSToDecimal`using`.apply()`,
//将'LocationFormatter'设置为'this',将'LongCombined'作为参数传递给'LocationFormatter.DMSToDecimal`
document.getElementById(“demo2”).innerHTML=LocationFormatter.DMSToDecimal.apply(LocationFormatter,LongCombined)代码>
这是一个使用变量
这是使用直接输入
jsidle尝试convertDMStoDD(parseInt(myvar))convertDMStoDD
defined在哪里?仍然给出了一个NaN响应。我添加了一个小提琴链接,如果你想查看它的话。它被用作一个例子。真正的代码在代码片段和小提琴中。我将更改它以清除任何混淆。字符串不是参数数组。。。
// set `LongCombined` as array ,
// to preserve individual item `type` ; e.g.; `Number` , `String`
var LongCombined = [81, 36.96, 0, 'W'];
// set `LocationFormatter` as `Object`
var LocationFormatter = {};
LocationFormatter.NORTH = 'N';
LocationFormatter.SOUTH = 'S';
LocationFormatter.EAST = 'E';
LocationFormatter.WEST = 'W';
LocationFormatter.roundToDecimal = function (inputNum, numPoints) {
var multiplier = Math.pow(10, numPoints);
return Math.round(inputNum * multiplier) / multiplier;
};
LocationFormatter.DMSToDecimal = function (degrees, minutes, seconds, hemisphere) {
var ddVal = degrees + minutes / 60 + seconds / 3600;
ddVal = (hemisphere == LocationFormatter.SOUTH || hemisphere == LocationFormatter.WEST) ? ddVal * -1 : ddVal;
return LocationFormatter.roundToDecimal(ddVal, 5);
};
document.getElementById("demo1").innerHTML = LocationFormatter.DMSToDecimal(81, 36.96, 0, 'W');
// call `LocationFormatter.DMSToDecimal` utilizing `.apply()`,
// set `LocationFormatter` as `this` , pass `LongCombined` as argument to `LocationFormatter.DMSToDecimal`
document.getElementById("demo2").innerHTML = LocationFormatter.DMSToDecimal.apply(LocationFormatter, LongCombined);