简体   繁体   中英

Why is my JavaScript not hoisting functions?

Me and a few friends are making a game in JavaScript, and it's getting really long, it is currently at 2942 lines long. A while ago I noticed that JavaScript wasn't properly hoisting my functions. I have tested this in Firefox, Chrome, and Safari and this is the case in all three. Does anyone know how to fix this?

You may have found a bug in all the engines. You should report that bug to the engine developers. Or, you could provide some code for us to look at, as described clearly here , which I'm sure you already looked at but may have forgotten. Excerpt:

...if your problem is with code you've written, you should include some . But don't just copy in your entire program! ...it likely includes a lot of irrelevant details that readers will need to ignore when trying to reproduce the problem.

Or, you could use a little-known technique called "debugging". I suggest you learn it. Often, the technique of "debugging" is done using a tool called a "debugger". For example, debuggers allow you to watch your code executing, and examine variables, and stop the code at a "breakpoint".

But you could also use the simple "divide and conquer" approach. If your 2,942-line program doesn't work, then boil to down to half of that, so 1,471 lines. If you still see the bug or unexpected behavior, then you know the problem was not in the 1,471 lines you removed, but rather in the 1,471 lines you kept. Then keep doing this until you find the problem. If you boil the code down to a small sample which still doesn't work the way you think it should, then it is at that point that you can post to Stack Overflow.

A related technique is incremental backtracking. If your code was working, but suddenly stopped, then remove the stuff you recently added. That is quite easy if you are using a version control system like git --which you are, right? Often you can find the source of the problem quite easily in the recently-added or recently-changed code.

See, the thing is, Stack Overflow is not designed to be a "we debug your code for you for free" service. It's designed to be a repository of interesting Q&A about programming. Your question is not interesting. It's not even well-formed. It's not even answerable, because you gave us no information. Even if you posted all 2,942 lines, it would still be a horrible question, because who is going to go through your thousands of lines looking for a bug? If someone did, and identified your bug, how would that answer help future visitors to SO, which is the ultimate reason for Stack Overflow's existence?

And perhaps more importantly, when you are trying to get your program working, asking people on SO to find your bugs is very slow and inefficient. You may have to wait for hours or even days, when with the right debugging techniques you could solve your own problem in five minutes. And remember that if you are looking for a job, and the interview question is about what kind of debugging techniques you use, no potential employer is going to hire you if your answer is that your main debugging technique is to post walls of code to SO and hope someone else finds your bug.

Hard to know why javascript isn't hoisting without an example of a function from your code that isn't getting hoisted. Javascript only hoists function declarations, not expressions.

Declaration:

function myFunction() {
  // I get hoisted!
}

Expression:

let myFunction = function () {
  // I don't get hoisted!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM