为什么这个 JavaScript 函数返回:“0:0function toString() { [native code] }”?

发布于 2024-09-30 23:52:04 字数 980 浏览 3 评论 0原文

我从此网站获取以下函数并将其插入进入我的代码以显示基于毫秒参数的用户友好的时间字符串。

为什么这个功能不起作用?

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぃ双果 2024-10-07 23:52:04

您忘记了对“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文