同一JS文件调用setNum函数出现模糊TypeError:"is not a function"
TypeError: "is not a function" for setNum() in Your MVC App Hey, let's brainstorm through this vague error you're hitting when calling setNum() in your ConverterController.js—since you're looking for collective ideas rather than step-by-step teaching, here are some angles to explore:
Ensure
setNumis properly accessible to the controller
Since you're following MVC,setNumis likely defined in eitherConverterModel.jsorConverterView.js. If you're using a module pattern (like IIFEs) to encapsulate your code, make sure the function is explicitly exposed so the controller can reach it. For example, in your model:// ConverterModel.js const ConverterModel = (function() { function setNum(num) { // Your logic here } // Expose the function to the global scope return { setNum: setNum }; })();If you're not using modules, double-check that
setNumisn't trapped in a local scope (like inside another function) that the controller can't access.Confirm script execution order matches load order
Even though you're loading scripts inConverterModel.js→ConverterView.js→ConverterController.jsorder, attributes likeasyncordeferon your<script>tags could throw off execution timing. Make sure the controller runs after the script containingsetNumhas fully parsed and executed. Also, ifsetNumrelies on DOM elements (if it's a view function), ensure you're not calling it before the DOM is ready.Check for accidental overwrites or naming collisions
It's easy to accidentally reassignsetNumto a non-function value somewhere in your code. Do a quick search across all three files to see if there's something like:// Oops—this turns setNum into a number, breaking the function call let setNum = 123;Also, watch for conflicts with global variables (like if another library uses the same function name).
jQuery-related edge cases (even if you don't suspect it)
You mentioned most similar issues tie back to jQuery. If your view uses jQuery, make sure you're not mixing jQuery objects with vanilla JS functions. For example:- If
setNumis a method meant to be called on a jQuery element, don't call it on a raw DOM node. - Check if you've redefined the
$alias (common if you're using other libraries alongside jQuery), which could break jQuery-related functions thatsetNummight depend on.
- If
内容的提问来源于stack exchange,提问作者whiteside0013

