JavaScript调用函数触发“不是函数”错误求助(未学调试工具)
displayInfo() Call Hey Lisa, sorry you're stuck on this frustrating error—let's walk through the most common causes and how to fix them even without dedicated debugging tools!
Common Causes & Fixes
Typos or incorrect function call syntax
Double-check that you're using the exact function name you defined (capitalization matters!displayinfo()vsdisplayInfo()won't work). Also make sure you're including the parentheses()when calling it—omitting them just references the function itself instead of running it.Syntax errors breaking your function definition
Looking at your code snippet, you havevar ...which suggests incomplete variable declarations. If there's a syntax mistake insidedisplayInfo()—like a missing semicolon, unclosed bracket, or malformed variable assignment—the browser might fail to fully parse the function. That meansdisplayInfowon't exist as a valid function at all.
Check the line number listed in your browser's error message, then go to that line in your code to fix obvious issues. For example, finish your variable declarations properly:var fullName = document.myForm.fullName.value; var int1 = parseInt(document.forms["myForm"]["int1"].value); var int2 = parseInt(document.forms["myForm"]["int2"].value); // Example of a complete declarationFunction is trapped in a local scope
If you wrappeddisplayInfo()inside another function (like an init function or event listener), it won't be accessible in the global scope where you're trying to call it. For example:// This won't work if you call displayInfo() outside init() function init() { function displayInfo() { // Your code here } } init();Fix this by moving
displayInfo()to the top level of your script so it's globally available.A variable is overriding your function name
If somewhere else in your code you have a variable nameddisplayInfo(likevar displayInfo = "";orlet displayInfo = 123;), it will replace the function reference. The browser will try to execute that variable as a function, hence the error. Search your code for any other uses ofdisplayInfoand rename the variable if needed.
Quick Tip for Debugging Without Tools
Your browser's console will tell you the exact line where the error occurs. Look for a message like "Uncaught TypeError: displayInfo is not a function" followed by a line number—jump straight to that line in your code to spot the issue faster.
内容的提问来源于stack exchange,提问作者Lisa

