Why does this JavaScript function return: "0: 0 toString () function {[native code]}"?

I took the following function from this site and connected it to my code to display a user-friendly temporary line based on a millisecond argument.

Why is this feature not working?

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 the following:

 0:0function toString() { [native code] }:0function toString() { [native code] } 
+4
source share
1 answer

You forgot () in your toString calls.

edit - sorry, I had to step back for a second. As @Gareth commented, references to "toString" are syntactically valid, as they are just function references. Thus, the analyzer has no problems with your code. What is wrong when you implicitly convert these links into strings.

If you just add () to each call, it should work much better. Or, since this page you are linking to points to a few posts further, you really don't need .toString() at all.

+6
source

All Articles