简体   繁体   English

如何将 Javascript 中的字符串变量放在一起? 它只是说“对象未定义”

[英]How do I put together string variables in Javascript? It's just saying "Object Undefined"

I have tried for so long to get this code to work.我已经尝试了很长时间才能使此代码正常工作。 I'm programming a little game when you need to be fast, so I made a stopwatch.当你需要快速时,我正在编写一个小游戏,所以我做了一个秒表。 But the stopwatch just doesn't want to work.但是秒表就是不想工作。 Instead of the seconds the stopwatch is showing Object Undefined and I don't know why.而不是秒表显示Object Undefined ,我不知道为什么。 This is the code i'm using:这是我正在使用的代码:

var stopwatchFrame = 0;
var stopwatchSeconds = 0;
var stopwatchSecondsString = "Nothing";

    stopwatchFrame+=1;
    stopwatchSeconds = floor(stopwatchFrame/updatesPerSecond);
    stopwatchSecondsString = toString(stopwatchSeconds);
    var = "Total time: " + stopwatchSecondsString + " seconds";

I'm using a simple website called Koda.nu , it's a Swedish website for young to learn programming in JS.我正在使用一个名为Koda.nu的简单网站,这是一个供年轻人学习 JS 编程的瑞典网站。 Some functions is coming from their built in source.一些功能来自它们的内置源。 I'm new to programming so that's why.我是编程新手,所以这就是原因。

You are missing a variable name where you have a value of "Total time: " + stopwatchSecondsString + " seconds";您缺少一个变量名称,其中您的值为"Total time: " + stopwatchSecondsString + " seconds"; It should be:它应该是:

var totalTime = "Total time: " + stopwatchSecondsString + " seconds";

Also read what @Jaromanda X wrote in the comments section.另请阅读@Jaromanda X 在评论部分中写的内容。 It should be like this:它应该是这样的:

stopwatchSeconds = Math.floor (stopwatchFrame/updatesPerSecond); stopwatchSeconds = Math.floor (stopwatchFrame/updatesPerSecond); stopwatchSecondsString = stopwatchSeconds.toString() ; stopwatchSecondsString = stopwatchSeconds.toString() ;

We don't have an access to your updatesPerSecond variable so that would throw an error as well.我们无权访问您的updatesPerSecond变量,因此也会引发错误。 If declared, your code would work like this:如果声明,您的代码将像这样工作:

var stopwatchFrame = 0;
var stopwatchSeconds = 0;
var stopwatchSecondsString = "Nothing";
var updatesPerSecond = 0;

stopwatchFrame += 1;
stopwatchSeconds = Math.floor(stopwatchFrame / updatesPerSecond);
stopwatchSecondsString = stopwatchSeconds.toString();
var totalTime = "Total time: " + stopwatchSecondsString + " seconds";

You dont have a variable name in the last line, and if this is all your code, then you dont initialize updatesPerSecond, meaning you dont have a line like你在最后一行没有变量名,如果这就是你的全部代码,那么你不初始化updatesPerSecond,这意味着你没有像

var updatesPerSecond = somenumberhere

If you name your last variable and initialize updatesPerSecond then you should be fine.如果你命名你的最后一个变量并初始化 updatesPerSecond 那么你应该没问题。

However I dont know anything about this website, but I quess it's old.但是我对这个网站一无所知,但我怀疑它已经过时了。 Here is some advice.这里有一些建议。

You need to tell javascript, that floor is a function from Math so use Math.floor , maybe it works in this website like you did, but keep in mind that you should use it otherwise.您需要告诉 javascript,该楼层是来自 Math 的 function 所以使用Math.floor ,也许它可以像您一样在这个网站上工作,但请记住,您应该以其他方式使用它。

toString() doesnt work like that. toString() 不能那样工作。 Again I dont know if they are using some different methods, but normal js toString() works like number .toString() and u can pass the radix as a parameter, meaning the base of the number representation (2 for binary, 16 for hexadecimal etc.) but this is optional, default is 10 for decimal.同样,我不知道他们是否使用了一些不同的方法,但是普通的 js toString() 像数字.toString() 一样工作,你可以将基数作为参数传递,这意味着数字表示的基数(2 表示二进制,16 表示十六进制等)但这是可选的,十进制默认为 10。

Dont use var as a declaration.不要使用 var 作为声明。 Use let instead, if the variable will change, and use const if it wont.如果变量会更改,请使用 let ,如果不会更改,请使用 const 。 In your case you should use let everywhere.在您的情况下,您应该在任何地方使用 let 。

Other thing is that you can use the ++ operator to increment a value by 1, so instead of stopwatchFrame+= 1 just use stopwatchFrame++另一件事是,您可以使用 ++ 运算符将值增加 1,因此不要使用stopwatchFrame+= 1而是使用stopwatchFrame++

And last you shouldn't initialize your default string value as "Nothing", it should be "", an empty string or undefined or null.最后,您不应该将默认字符串值初始化为“Nothing”,它应该是“”、空字符串或未定义或 null。

I hope this helps, have a good day!我希望这会有所帮助,祝你有美好的一天!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将包含附加到 URL 的变量的 arrays 放入图像占位符中? JavaScript - How do I put arrays which consist of variables that are attached to URL's into image placeholders? JavaScript 为什么我总是收到错误消息,指出对象属性未定义? - Why do I keep getting an error saying an object property is undefined? 我如何在Firefox中使用Angularjs中的事件,因为它表示事件未定义? - How do I use event in Angularjs with firefox as it's saying that event is undefined? 如何将字符串放在javascript变量和“ echo”变量中 - How can I put a string inside a javascript variable and “echo” variables in it 如何将javascript变量组合成一个对象 - How do I combine javascript variables into a object 如何将 2 个 javascript 变量组合成一个字符串 - How do I combine 2 javascript variables into a string 如何将变量放入 javascript 字符串中? - How do I put variables inside javascript strings? JavaScript中的React API对象总是说未定义 - React API object in JavaScript keeps saying undefined 如何将 2 个功能放在一起并使其在 javascript 和 html5 中按我的意愿工作 - how do I put 2 functions together and make it work as I want in javascript and html5 我如何完全解构这个 JavaScript 对象,它一直返回 Undefined 但有分配给它的值 - How do I destructure this JavaScript Object completely, It keeps returning Undefined yet there`s a value assigned to it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM