为什么这个 JavaScript 函数返回:“0:0function toString() { [native code] }”?
我从此网站获取以下函数并将其插入进入我的代码以显示基于毫秒参数的用户友好的时间字符串。
为什么这个功能不起作用?
function getTimeFromMillis(millis)
{
milliSecs = millis;
msSecs = (1000)
msMins = (msSecs * 60)
msHours = (msMins * 60)
numHours = Math.floor(milliSecs/msHours)
numMins = Math.floor((milliSecs - (numHours * msHours)) / msMins)
numSecs = Math.floor((milliSecs - (numHours * msHours) - (numMins * msMins))/ msSecs)
if (numSecs < 10){
numSecs = "0" + numSecs.toString
}
if (numMins < 10){
numMins = "0" + numMins.toString
}
resultString = numHours + ":" + numMins + ":" + numSecs
return resultString;
}
如果我从调用函数传递一个毫秒值,我会得到:
0:0function toString() { [native code] }:0function toString() { [native code] }
I took the following function from this site and plugged it into my code to display a user-friendly time string based on a millisecond argument.
Why does this function not work?
function getTimeFromMillis(millis)
{
milliSecs = millis;
msSecs = (1000)
msMins = (msSecs * 60)
msHours = (msMins * 60)
numHours = Math.floor(milliSecs/msHours)
numMins = Math.floor((milliSecs - (numHours * msHours)) / msMins)
numSecs = Math.floor((milliSecs - (numHours * msHours) - (numMins * msMins))/ msSecs)
if (numSecs < 10){
numSecs = "0" + numSecs.toString
}
if (numMins < 10){
numMins = "0" + numMins.toString
}
resultString = numHours + ":" + numMins + ":" + numSecs
return resultString;
}
If I pass it a millisecond value from my calling function I get this:
0:0function toString() { [native code] }:0function toString() { [native code] }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您忘记了对“toString”的调用中的
()
。编辑 - 抱歉,不得不离开一会儿。正如@Gareth 评论的那样,对“toString”的引用在语法上是有效的,因为它们只是对函数的引用。因此解析器对您的代码没有任何问题。问题在于当您将这些引用隐式转换为字符串时。
如果您只是将
()
添加到每个调用中,它应该会工作得更好。或者,正如您链接到的那个页面指出了下面的一些帖子,您实际上根本不需要.toString()
。You forgot the
()
in your calls to "toString".edit — sorry had to step away for a sec. As @Gareth commented, the references to "toString" are syntactically valid, as they're just references to functions. Thus the parser has no problems with your code. What goes wrong is when you implicitly convert those references to strings.
If you just add
()
to each call, it should work a lot better. Or, as that very page you linked to points out a few posts further down, you really don't need.toString()
at all.